私はこの質問に答えるためにしようとしていた。emberjs: add routes after app initialize()Ember.Object.reopen()で遊んでいるのはなぜですか?
私はそれがどのように動作するかを理解するために、)Ember.Object.reopen(と遊び始め、おそらく前の質問に答えるの方法を見つけます。
私は少し戸惑いを感じており、このコードの動作を理解していない:
jsfiddle:http://jsfiddle.net/Sly7/FpJwT/
<script type="text/x-handlebars">
<div>{{App.myObj.value}}</div>
<div>{{App.myObj2.value}}</div>
<div>{{App.myObj3.value}}</div>
</script>
App = Em.Application.create({});
App.MyObject = Em.Object.extend({value: 'initial'});
App.set('myObj', App.MyObject.create());
Em.run.later(function(){
App.get('myObj').reopen({
value: "reopenOnInstance"
}); // the template is not updated, 'initial' is still diplayed, but
console.log(App.get('myObj').get('value')); // print 'reopenOnInstance'
App.MyObject.reopen({
value: "reopenOnClass"
});
App.set('myObj2',App.MyObject.create()); // the template is updated and
console.log(App.get('myObj2').get('value')); //print 'reopenOnClass'
App.myObj3 = App.MyObject.create(); // the template is not updated but
console.log(App.myObj3.get('value')); // print 'reopenOnClass'
Em.run.later(function(){
App.get('myObj').set('value', "setWithSetter"); // the template is updated and
console.log(App.get('myObj').get('value')); // print 'setWithSetter'
App.get('myObj2').set('value', "setWithSetter"); // the template is updated and
console.log(App.get('myObj2').get('value')); // print 'setWithSetter'
App.myObj3.set('value', "setWithSetter"); // the template is not updated but
console.log(App.myObj3.get('value')); // print 'setWithSetter'
}, 2000);
},2000);
誰かが何が起こっているかを説明することができた場合は、特に理由テンプレートは時々更新されず、ときどき更新され、あるクラスでreopen
を呼び出して呼び出す場合と、インスタンスで呼び出す場合の違いは何ですか?
すべて意味があります。大変感謝してくれてありがとう。ですから、私がここでやっているようにインスタンスを再オープンしても、App.get( 'myObj')と同じ動作をします。value = 'reopenOnInstance'' right? 私はgetPath、メソッドを知っていましたが、最新のemberではgetに同じ動作があり、 'obj.get( 'otherObj.someProperty')'を実行できます。 –
編集を参照してください。これは、あなたが正しいことを意味します。値を直接設定することは、インスタンスの再オープンのようになります。しかし、あなたが何かその値(UIなど)にバインドしている場合、Emberは「set」が使用されなかったためにエラーをスローします([phiddler](http://jsfiddle.net/scispear/n5B5d/)エラー)。 – SciSpear
もう一度編集していただきありがとうございます。私は解明すべきことがもうないので、答えは完了したと思います。 –