更新集合中的模型

发布于 2024-12-21 09:22:12 字数 758 浏览 1 评论 0原文

我的问题是如何更新集合中的模型?这就是我正在做的事情。在页面加载时,我获取联系人列表。在一个视图中,我在无序列表中列出了这些联系人。每个联系人都是可点击的,这将带您进入编辑表单。对联系人进行更改后,您可以保存该联系人。这将带您到将更改后的模型保存回集合中的方法。你会怎么做?在主干文档中没有更新方法(或者至少我没有看到它)。我创建了一种方法来执行此操作,但我不确定它是否是首选的骨干方式。在这里:

        updatePlan : function()
        {
            //unique ID of the model
            var id = $( 'input[ name=id ]' ).val();
            //remove the old model from the collection
            this.collection.remove( this.model );
            //add the updated model to the collection
            this.collection.add( this.model );              

        }

您会认为会有这样的功能:

        updatePlan : function()
        {
            this.collection.update( this.model );

        }

谢谢您的帮助

My question is how would you update a model in a collection? Here is what I am doing. On page load I fetch a list of contacts. In one view I list out these contacts in an unordered list. Each contact is clickable which will take you to an edit form. Once you make changes to the contact you can save the contact. This will take you to a method that save the altered model back to the collection. How would you do this? In the backbone docs there isn't an update method (or at least I don't see it). I created a way to do this but I am not sure if it is the preferred Backbone way. Here it is:

        updatePlan : function()
        {
            //unique ID of the model
            var id = $( 'input[ name=id ]' ).val();
            //remove the old model from the collection
            this.collection.remove( this.model );
            //add the updated model to the collection
            this.collection.add( this.model );              

        }

You would think there would be a function like this:

        updatePlan : function()
        {
            this.collection.update( this.model );

        }

Thanks for the help

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

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

发布评论

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

评论(2

北渚 2024-12-28 09:22:12

您可以让您的视图允许用户编辑联系人并为您更新模型。由于模型是通过引用传递的,因此它也将在它所属的集合中进行更新。

这是一个例子。

EditView = Backbone.View.extend({
  events: {
    'change #name': 'nameChanged',
    'change #age': 'ageChanged'
  },
  render: function() {
    //render code
  },
  nameChanged: function() {
    var nameValue = this.$('#name').val();
    this.model.set({ Name : nameValue });
  },
  ageChanged: function() {
    var ageValue = this.$('#age').val();
    this.model.set({ Age : ageValue });
  }
}

然后,当您创建编辑视图时,您从集合中传入选定的模型。

selectedContact = contactCollection.at(0);
var editView = new EditView({ model: contact });
$('#editDiv').html(editView.render().el);

剩下要做的唯一一件事就是当用户单击“保存”按钮时将模型保存到服务器。

$('#saveButton').click(function() {
  selectedContact.save();
});

You can have your view that allows the user to edit the contact update the model for you. And since the model gets passed by reference, it will also be updated in the collection that it is a part of.

Here's an example.

EditView = Backbone.View.extend({
  events: {
    'change #name': 'nameChanged',
    'change #age': 'ageChanged'
  },
  render: function() {
    //render code
  },
  nameChanged: function() {
    var nameValue = this.$('#name').val();
    this.model.set({ Name : nameValue });
  },
  ageChanged: function() {
    var ageValue = this.$('#age').val();
    this.model.set({ Age : ageValue });
  }
}

And then when you create the edit view, you pass in the selected model from the collection

selectedContact = contactCollection.at(0);
var editView = new EditView({ model: contact });
$('#editDiv').html(editView.render().el);

The only thing left to do is to save the model to the server when the user clicks the save button.

$('#saveButton').click(function() {
  selectedContact.save();
});
倾`听者〃 2024-12-28 09:22:12

我同意保罗的观点,你应该有一个用于你的收藏的视图,一个用于你的模型的视图。
模型的视图允许您编辑模型,并且保存应该正确传播。

不过,这表明您似乎有一个视图,其中有 1 个模型和 1 个集合(基于您的删除和添加 this.model),因此您有一个显示单个模型的视图...我认为:D

然后您可以这样做这。

    updatePlan : function()
    {
        //unique ID of the model
        var id = $( 'input[ name=id ]' ).val();
        this.model.set({id: id});
        this.model.save();
    }

我认为问题可能只是您最初的“逻辑”,您不编辑集合中的模型。
您编辑一个模型,该模型恰好属于一个集合。
因此,模型和 model.collection 都会触发事件,就像在您的 id 集上一样,Change:id 事件将触发并触发到您的模型及其集合的任何绑定

希望它有帮助:)

I agree with Paul, you should have a View for your collection, and a View for your model.
The view for the model allows you to edit your model and the save should propagate up correctly.

However that said you seem to have a view there that has 1 model and 1 collection (based on your remove and add this.model) so you have a view that is displaying a single model... I assume :D

You could then do this.

    updatePlan : function()
    {
        //unique ID of the model
        var id = $( 'input[ name=id ]' ).val();
        this.model.set({id: id});
        this.model.save();
    }

I think the issue might then just be your initial "logic", you don't edit a model within a collection.
You edit a model, that model just happens to belong to a collection.
So events will fire for both the model and the model.collection, like on your set of id, the Change:id event will fire and trigger any binding to either your model its collections

Hope it helps :)

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