gpt4 book ai didi

javascript - 简单的backbone.js模型连接

转载 作者:行者123 更新时间:2023-12-02 19:34:13 24 4
gpt4 key购买 nike

我从主干开始,尝试创建一个简单的 View ,每当我的模型发生变化时就会发出警报。现在正在调用 View 中的初始化函数,但是当我的模型更改时(我的模型正在更改),渲染函数没有被调用。我尝试了两种绑定(bind)到更改事件的方法(在初始化函数和事件属性中)。我觉得我错过了一些明显的东西。

#jsonPreview ID 存在于 html 中。

// Create the view
var JSONView = Backbone.View.extend({
initialize: function(){
this.bind("change", this.render);
},

render: function() {
alert("change");
},
events:
{
"change":"render"
}

});

// Create the view, and attach it to the model:
var json_view = new JSONView({ el: $("#jsonPreview"), model: documentModel });

提前致谢。

最佳答案

看起来您正在绑定(bind)到 View 上的 change 事件,而不是 View 的模型。认为您需要绑定(bind)到模型事件,如下所示:

initialize: function(){
this.model.bind("change", this.render);
},

关于javascript - 简单的backbone.js模型连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11124338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com