Backbone.js Handling of Attributes That Are Arrays

后端 未结 4 582
迷失自我
迷失自我 2020-12-13 06:44

I really like Backbone, but I am having the hardest time doing what would seem to be simple things. I appreciate any help with the following example.

I have a model,

4条回答
  •  伪装坚强ぢ
    2020-12-13 07:14

    To be clear, the last option provided by Maksym H. will not solve the problem. The defaults property is provided assuming all of the values set are immutable. An array, however, is mutable, meaning its value can be changed (e.g. tags[0] = "hello" can be changed with tags[0] = "hi there").

    By using btford's answer, you are forcing a new instance of any mutable object/property to be created on every new instance of the model, so it is never shared, because the object is created with a function scoped variable.

    Similarly, Derick Bailey's answer is correct, it just uses the initialize method instead of the defaults method.

提交回复
热议问题