3

私はモデルを持っています:

app.ObjectOne = Em.Object.extend({
    id: null,
    count: null
});

そして、プロパティ「SomeProperty」を計算した別のモデルは、ObjectOne のプロパティ「count」に依存したい

app.ObjectTwo = Em.Object.extend({
    id: null,
    someProperty: function(){
      return count+5;
    }.property('app.SomeObjectController.count')

});

それで、私はそのようにすることができますか?または、それを行う他の方法はありますか。

主なアイデアは、ObjectOne モデルのデータが ObjectTwo の後に来るということです。そのため、プロパティを再計算してビューを再レンダリングする必要があります。

4

1 に答える 1

2

私がよく理解していれば、あなたが望む動作はまさに Ember.js がもたらすものです。

まず、非常に基本的なテンプレートを次に示します。

<script type="text/x-handlebars">
  Here, the template is updated each time this property is updated (equivalent to bound property)
  {{App.objectTwo.someProperty}}
</script>​

これがジャバスクリプトです。ここで本当に ObjectController を使いたいのかわかりませんが、あなたが言及したので、私はそれを使用しました。ObjectController は、そのcontentプロパティのプロキシとして機能します。これは、 someObjectController.get('count') がcountコントローラ自体からプロパティを取得しようとすることを意味し、プロパティが存在しない場合、コントローラはcountそのコンテンツからプロパティを取得します。

App = Ember.Application.create();
App.someObjectController = Ember.ObjectController.create();

App.ObjectOne = Em.Object.extend({
  id: null,
  count: null
});
App.objectOne = App.ObjectOne.create({
  id: 1,
  count: 42
});
App.someObjectController.set('content', App.objectOne);

App.ObjectTwo = Ember.Object.extend({
  id: null,
  someProperty: function(){
    return App.someObjectController.get('count')+5;
  }.property('App.someObjectController.count')
});
App.objectTwo = App.ObjectTwo.create({
  id: 1
});

//in 3 seconds the count property is set to 0. You can see the template is updated
Ember.run.later(function(){
   App.objectOne.set('count', 0);
},3000);

動作中の jsfiddle は次のとおりです: http://jsfiddle.net/Sly7/M3727/4/

于 2012-10-19T17:41:05.910 に答える