1

In Ember, this is what I do:

I declare these objects

MyObject = Ember.Object.extend({
   //some serialization logic
});

FormViewContainer = MyObject .extend({
   type: "FormViewContainer",
   label: "",
   options: {
       css: "",
       class: "",
       attr: []
   },
  items:[]
});

FormViewField = MyObject.extend({
   type: "FormViewLibreField",
   isCodif: false,
   question: {},
   answer: undefined,
   options: {
      css: "",
      class: "",
      attr: []
   }
});

Then in My app:

var cont = FormViewContainer.create({
            label: "My Label",
            options: {
                class: "cssclassestuff"
            }
        });

Data.Questions.forEach(function (c) {
            var field = FormViewField.create();
            field.set("question",c);
            cont.get('items').push(field);
        });

//items are in the array, they are 6!
cont.get('items').length == 6

Thats the part I dont get. I create a new instance (supposedly), but i

FormViewContainer.create().get('items').length == 6

Object.Create() actually copied items from the other instance into this new one via my object constructor!

It only copied the "items" property, label and options are not affected.

I tried assigning properties with/without the get/set accessors and got the same result.

If anyone has an Idea?

4

1 回答 1

3

Ember 有这个奇怪的问题(?)/实现。任何定义为数组的东西都在实例之间共享。

 FormViewContainer = MyObject .extend({
    type: "FormViewContainer",
    label: "",
    options: {
        css: "",
        class: "",
        attr: []
    },
   items:[]
 });

做这个

 FormViewContainer = MyObject .extend({
    init: function(){
      this._super();
      this.items = [];
    },
    type: "FormViewContainer",
    label: "",
    options: {
        css: "",
        class: "",
        attr: []
    },
   items:null
 });
于 2013-10-18T09:48:07.037 回答