2009-10-13 3 views
1

ActionScript에서 MXML-esque "바인딩 문자열"을 지정할 수 있습니까?플렉스 : 액션 스크립트에서 MXML-esque "바인딩 문자열"에 바인딩 하시겠습니까?

MXMLBinding(this, "first_item", 
      this, "{myArrayCollection.getItemAt(0)"); 
MXMLBinding(this, ["nameLbl", "text"], 
      this, "Name: {somePerson.first} {somePerson.last}"); 

편집 :

예를 들어, 내가 좋아하는 뭔가를 할 수 있도록하려면이 작업을 수행 할 수없는 것처럼 응답과 제안 ... 감사는 기본적으로 보인다. 나는 주변을 파고 왜 그랬는지 알아 냈다. ,

… 
    BindingUtils.bindSetter(updateName, this, ["somePerson", "first"]); 
    BindingUtils.bindSetter(updateName, this, ["somePerson", "last"]); 
… 
protected function updateName(...ignored):void { 
    this.nameLbl.text = "Name: " + somePerson.first + " " + somePerson.last; 
} 

arrayCollection.getItemAt(0)에 바인딩, 조금 못생긴 ... 그리고 최초의 예는 다음과 같습니다

답변

0

좋아, 그래서 좀 파고를 수행하고, 여기 있는거야했습니다.

MXML에서의 바인딩은 컴파일 타임에 Java 코드 (modules/compiler/src/java/flex2/compiler/as3/binding/DataBindingFirstPassEvaluator.java, 내가 잘못하지 않은 경우)로 설정합니다.

는 예를 들어,이 바인딩 : first_item="{myArrayCollection.getItemAt(0)을} '', 무엇보다도으로 확장이되어

// writeWatcher id=0 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher shouldWriteChildren=true 
    watchers[0] = new mx.binding.PropertyWatcher("foo", 
               { propertyChange: true }, // writeWatcherListeners id=0 size=1 
               [ bindings[0] ], 
               propertyGetter); 

    // writeWatcher id=1 shouldWriteSelf=true class=flex2.compiler.as3.binding.FunctionReturnWatcher shouldWriteChildren=true 
    watchers[1] = new mx.binding.FunctionReturnWatcher("getItemAt", 
                 target, 
                 function():Array { return [ 0 ]; }, 
                 { collectionChange: true }, 
                 [bindings[0]], 
                 null); 

    // writeWatcherBottom id=0 shouldWriteSelf=true class=flex2.compiler.as3.binding.PropertyWatcher 
    watchers[0].updateParent(target); 

    // writeWatcherBottom id=1 shouldWriteSelf=true class=flex2.compiler.as3.binding.FunctionReturnWatcher 
    // writeEvaluationWatcherPart 1 0 parentWatcher 
    watchers[1].parentWatcher = watchers[0]; 
    watchers[0].addChild(watchers[1]); 

이것은 런타임 때문에에 설치 곱슬 중괄호 MXML 스타일 바인딩에 단순히 불가능하다는 것을 의미한다 코드는 ActionScript에서 존재하지 않는 수행하는

0

나는 BindingUtils 또는 ChainWatcher 사용할 수 있지만 나는 이런 식으로 뭔가를 보이는 코드로 끝날 것 더 나쁘다.

+1

중괄호 {} 바인딩은 MXML에만 적용되며 순수 AS3에서는 사용할 수 없습니다. BindingUtils는 갈 길입니다. – joshtynjala

0

ChangeWatcher를 사용하면 (예 : BindingUtils.bindProperty 또는 .bindSetter를 통해) 이동하는 방법입니다. 나는 이상한 표기법을 인정하지만 일단 익숙해지면 이해가되고 완벽하게 작동하며 매우 유연합니다.

물론 표기법이 당신을 도청했다면 어떻게 든 그 기능을 스스로 감쌀 수 있습니다. 두 가지 방법 모두 정적입니다. 따라서 응용 프로그램에 더 적절하다고 느껴지는 방식은 매우 간단합니다.

+0

표기법이 이상하지 않은 것은 아닙니다 ... 똑같은 것을 성취하는 데 훨씬 더 많은 코드가 필요하다는 것입니다. –

+0

그래, 나는 실제로 다른 방법이 있다고 생각하지 않는다. –

0

BindingUtils.bindSetter 메서드의 첫 번째 매개 변수 (함수)가 익명 메서드를 허용합니까?

BindingUtils.bindSetter(function() 
    { 
    this.nameLbl.text = "Name: " + somePerson.first + " " + somePerson.last; 
    }, this, ["somePerson", "last"]); 

나는 익명의 방법을 싫어하고 분명히 훨씬 더 추악한입니다 - 그래서 그것을 작동하는 경우에도 작동하는지,하지만 그냥 궁금 것을 권장하지 않습니다.

0

아무도 대답을 듣고 싶지 않지만, ActionScript에서 getters/setters로이 물건을 관리하십시오. 적절한 MVC를 사용하면 디스플레이 필드를 수동으로 설정하기가 쉽지 않습니다.

public function set myArrayCollection(value:Array):void { 
    myAC = new ArrayCollection(value); 
    first_item = mcAC.getItemAt(0); // or value[0]; 
} 

등 ....

+0

어려움이 있습니다 만, 이것은 myAC [0]'에 바인드되지 않습니다 ... 그러려면, myAC에'ChangeListener'를 추가해야합니다.). –

+0

필자는 프로그래머가 오랫동안 바인딩하지 않고 관리 해왔다는 것이 중요하다고 생각합니다. 일반적으로 좋은 아키텍처로 충분합니다. AC 내부의 변경 사항에 대응할 수있는 알림 시스템이 필요할 수 있습니다. – Glenn

2

(뻔뻔 플러그)

BindageTools가이 작업을 수행 할 수 있습니다.

Bind.fromProperty(this, "myArrayCollection", itemAt(0)) 
    .toProperty(this, "first_item"); 

Bind.fromAll(
    Bind.fromProperty(this, "somePerson.first"), 
    Bind.fromProperty(this, "somePerson.last") 
    ) 
    .format("Name: {0} {1}") 
    .toProperty(this, "nameLbl.text"); 

BindageTools는 원본 개체를 먼저 넣고 대상을 마지막으로 넣습니다 (BindingUtils는 대상을 먼저두고 원본은 마지막으로 넣음).

관련 문제