开发者

Model 'changing', but it's not

开发者 https://www.devze.com 2023-04-12 08:58 出处:网络
I have a view: class FancyView extends Backbone.View template: #fancytemplate initialize: () -> @add()

I have a view:

class FancyView extends Backbone.View
    template: #fancytemplate

    initialize: () ->
        @add()
        @model.bind('change', @update)

    add: () ->
        $(@el).html($(@template).tmpl(@model.toJSON())).prependTo('#fancy')

    update: () ->
        $(@el).html($(@template).tmpl(@model.开发者_如何学GotoJSON()))

When a change comes in logging @model.changedAttributes() in update shows changes under data but logging @model still shows the old data and thus nothing changes on update.

Why is @model still the old data?


It would be nice to see more... what does the template look like? What templating engine are you using?

I can say that I see one problem off the bat... you need to use the "fat arrow" (=>) instead of (->) for your update function. If you don't, @el, @template and @model will be in the wrong context when the event fires.

update: =>
    $(@el).html($(@template).tmpl(@model.toJSON()))
0

精彩评论

暂无评论...
验证码 换一张
取 消