문제

I have an itemController that has two fields: isExpanded and newComment. The isExpanded is working great and binding to each itemController, but the newComment is somehow shared by all the instances of the itemController. And it will only share for the first time. After I submit the new comment, that textarea no longer share the same binding with other textareas, but the rest of the textareas are still mysteriously binding to the same ghost newComment. Why?

http://emberjs.jsbin.com/gojuhega/1/edit

[1]Previous question removed and isolated to this jsbin.

도움이 되었습니까?

해결책

It seems when new controllers are created based on DiscussionController prototype, its properties are shallow-copied. Since your newComment is a reference type, you end up with 3 controllers all sharing a same instance. Only when you click 'Create' do individual controllers get their own instances.

One potential solution:

App.DiscussionController = Ember.Controller.extend({
  isExpanded: false,

  newComment: null,
  actions: {
    toggleExpand: function(){
      this.toggleProperty('isExpanded');
      if (!this.get("newComment")) {
        this.set("newComment", {body:""});
      }
    },
    createComment: function() {
      this.set('newComment', {
        body: ''
      });
    }
  }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top