1

モデルにあるすべてのレコードのコンポーネントを作成するテンプレートがあります。コンポーネントを見つけて、他のテンプレートからのイベントに基づいて実行時にそのプロパティの 1 つを更新したいと考えています。DOM に挿入された特定のコンポーネントを見つける方法。{{#each}} {{私の名前}} {{/each}}

<script type="text/x-handlebars" data-template-name="components/my-name">
Hi, my name is {{name}}.
</script>

var App = Ember.Application.create();
App.IndexRoute=Ember.Route.extend({
model:function(){
 return dummyNames;
} 
});
var dummyName={[name='John', name='Jane', name='Jade']};

このコードは、画面に名前を表示します。これで、change という別のテンプレートができました。

<script type="text/x-handlebars" data-template-name="change">
<button {{action 'changeNow'}}></button>
</script>

App.ChangeController=Ember.Controller.extend({
    actions:{
        changeNow:function(){
           //Here I want to find the component where the name is Jane and change it to Kate. How         to do this?
        }
    }
});
4

1 に答える 1

0

これが私があなたのために用意したjsbinです。

http://emberjs.jsbin.com/sanukoro/3/edit

コンポーネントには独自のスコープがあります。{{name}} がコンポーネントに表示されている場合は、インデックス テンプレートでレンダリングしたと仮定します。つまり、そのプロパティをコンポーネントに渡しました。ガイドからこのようなもの

{{blog-post title=name}}

プロパティをコンポーネントに渡す

したがって、基本的に変更したいすべてのプロパティは IndexController のものです。

IndexController プロパティ (ダミー名) を直接変更すると、データ バインディングのために変更がコンポーネントに反映されます。

異なるコントローラーでまとめて実行したいので、ChangeController で IndexController への依存を宣言する必要があります。 依存関係の管理

App.ChangeController=Ember.Controller.extend({
    needs: ['index'] //dependency
    names: Ember.computed.alias("controllers.content") //now get properties of Index controller
    actions:{
        changeNow:function(){
          //here you can find name jane and replace it with to kate

        }
    }
});

変更を詳細に追跡するには、addArrayObserver を確認する必要がある場合があります。

于 2014-02-26T09:55:00.173 に答える