无论如何我们可以避免在少数情况下运行可观察代码?
我怎么试过?
我发现要避免的唯一方法是将新属性作为标志添加到视图中,如果在运行可观察方法代码之前进行检查。
这是基本的 的jsfiddle link am提供基本的观察者功能
HTML
<script type="text/x-handlebars" data-template-name="application">
{{view MyApp.MyContainerView name="Santa Claus"}}
</script>
<script type="text/x-handlebars" data-template-name="foo">
{{view.testProp}}
</script>
JS
MyApp = Ember.Application.create({
autoinit: false
});
MyApp.router = Ember.Router.create({
root: Ember.Route.extend({
index: Ember.Route.extend({
route: '/'
})
})
});
MyApp.ApplicationController = Ember.Controller.extend({});
MyApp.MyContainerView = Em.ContainerView.extend({
childViews: ['foo'],
foo: Em.View.extend({
testProp: 100,
testPropObservable: function(){
console.log("Prop Observed");
}.observes('testProp'),
init: function() {
this._super();
this.set('testProp', 200);//i want to avoid obeserver here
},
templateName: 'foo'
})
});
MyApp.initialize(MyApp.router);
一种替代方法是在运行时添加/删除观察者。鉴于上面的示例,您可以通过调用阻止在init()期间触发观察者 this.addObserver
在值初始化之后
foo: Em.View.extend({
testProp: 100,
testPropDidChange: function(){
console.log("Value changed to: ", this.get('testProp'));
},
init: function() {
this._super();
this.set('testProp', 200);//i want to avoid obeserver here
this.addObserver('testProp', this.testPropDidChange)
},
templateName: 'foo'
})
有关工作示例,请参阅此jsfiddle: http://jsfiddle.net/NSMj8/1/
在ember指南中有一个很好的观察者概述: http://emberjs.com/guides/object-model/observers/
有关如何添加/删除观察者的更多信息,请参阅Ember.Observable的API文档: http://emberjs.com/api/classes/Ember.Observable.html
一种替代方法是在运行时添加/删除观察者。鉴于上面的示例,您可以通过调用阻止在init()期间触发观察者 this.addObserver
在值初始化之后
foo: Em.View.extend({
testProp: 100,
testPropDidChange: function(){
console.log("Value changed to: ", this.get('testProp'));
},
init: function() {
this._super();
this.set('testProp', 200);//i want to avoid obeserver here
this.addObserver('testProp', this.testPropDidChange)
},
templateName: 'foo'
})
有关工作示例,请参阅此jsfiddle: http://jsfiddle.net/NSMj8/1/
在ember指南中有一个很好的观察者概述: http://emberjs.com/guides/object-model/observers/
有关如何添加/删除观察者的更多信息,请参阅Ember.Observable的API文档: http://emberjs.com/api/classes/Ember.Observable.html