backbone.js视图确定模型的哪个属性被改变

发布于 2024-12-23 14:39:57 字数 745 浏览 0 评论 0原文

我如何知道渲染函数中视图模型的哪个属性被更改? (在渲染函数中,“e”是模型,但我只需要更改的属性。)我需要知道这一点才能知道要使用哪个模板。或者还有其他方法可以做到这一点吗?

window.Person = Backbone.Model.extend({});

window.Njerzit = Backbone.Collection.extend({
    model: Person,
    url: '/Home/Njerzit'
});

window.PersonView = Backbone.View.extend({
    tagName: 'span',

    initialize: function () {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },

    render: function (e) {
        //if model name is changed, I need to render another template
        this.template = _.template($('#PersonTemplate').html());
        var renderContent = this.template(this.model.toJSON());
        $(this.el).html(renderContent);
        return this;
    }
});

How can I know which attribute of the view model is changed in the render function? (In the render function, "e" is the model, but I need only the attribute which is changed.) I need to know this to know which template to use. Or is there another method to do this?

window.Person = Backbone.Model.extend({});

window.Njerzit = Backbone.Collection.extend({
    model: Person,
    url: '/Home/Njerzit'
});

window.PersonView = Backbone.View.extend({
    tagName: 'span',

    initialize: function () {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },

    render: function (e) {
        //if model name is changed, I need to render another template
        this.template = _.template($('#PersonTemplate').html());
        var renderContent = this.template(this.model.toJSON());
        $(this.el).html(renderContent);
        return this;
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-12-30 14:39:57

我相信 changedAttributes 函数就是您正在寻找的

changedAttributesmodel.changedAttributes([属性])
仅检索已更改的模型属性的哈希值。可选地,
可以传入外部属性哈希,返回属性
在与模型不同的哈希值中。这可以用来计算
指出应该更新视图的哪些部分,或者需要更新哪些调用
将更改同步到服务器。

或者使用 hasChanged 函数检查特定属性是否已更改

hasChangedmodel.hasChanged([属性])
自上次“更改”事件以来模型是否已更改?如果传递了属性,则返回 true
如果该特定属性已更改。

var nameChanged = this.model.hasChanged("name");

I believe the changedAttributes function is what you're looking for

changedAttributesmodel.changedAttributes([attributes])
Retrieve a hash of only the model's attributes that have changed. Optionally,
an external attributes hash can be passed in, returning the attributes
in that hash which differ from the model. This can be used to figure
out which portions of a view should be updated, or what calls need to
be made to sync the changes to the server.

or to check if a specific attribute has changed use the hasChanged function

hasChangedmodel.hasChanged([attribute])
Has the model changed since the last "change" event? If an attribute is passed, returns true
if that specific attribute has changed.

var nameChanged = this.model.hasChanged("name");
乄_柒ぐ汐 2024-12-30 14:39:57

如果您只想通知名称是否已更改,则可以绑定到 change:namehttp ://documentcloud.github.com/backbone/#Model-set

You can bind to change:name if you only want to notify if the name has changed: http://documentcloud.github.com/backbone/#Model-set

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文