如何更新一组使用Backbone.js的&放记录; Rails的? [英] How to update a set of records using Backbone.js & Rails?

查看:105
本文介绍了如何更新一组使用Backbone.js的&放记录; Rails的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Rails和放大器的通知; Backbone.js的应用

I have notifications in my Rails & Backbone.js app

// MODEL
NotificationModel = App.BB.Model.extend({
    defaults : {}
});


// COLLECTION
NotificationCollection = App.BB.Collection.extend({
    model: NotificationModel,
    url: '/notifications',

    initialize : function() {
        var me = this;
        me.fetch();
    }

});

该模式具有以下字段(ID,读),而读是真还是假。

The model has the following fields (id, read) where read is true or false.

我想要做的是,一旦用户查看通知,标记为已读=服务器上的真实。什么是做与Backbone.js的正确方法是什么?

What I want to do is once the user views the notifications, mark all as READ = true on the server. What's the right way to do that with Backbone.js?

感谢

推荐答案

有在Backbone.js的无批量上传
所以你有两个选择

There is no Bulk upload in Backbone.js so you have two choices


  1. 继续努力为骨干做得最好,
    你可以你的收藏设置的项目在更新每个模型读取。

  1. continue to work as backbone does best, you can update every model in your collection to set the item as read.

myCollection.each(function(note){
    note.set({read: true});
    note.save();
});

这将发布模​​型到服务器和更新服务器(即型号)
 是的,这似乎是沉重的,但骨干以这种方式工作,有一个模型没有批量更新(没有?)

this will post the model to the server and update your server (for that model only) yes, this might seem heavy but backbone works this way, there is no bulk update of models (yet?)

其他的解决办法是将它张贴自己usign做Ajax调用自定义,
比如这个jQuery电话。

the other solution is to post it yourself usign a custom made Ajax call, for example this jQuery call.

var oData = {models : myCollection.toJSON() };

$.ajax({
    type: "POST",
    url: "my/backend/url.php",
    data: oData
}).done(function( msg ) {
    alert( "Data Saved: " + msg );
    // your code after the post is completed...
});


记住:在此之后Ajax调用完成后,你可能要再次读取您的收藏来更新它(否则你的收藏中的车型将不知道哟ü没有到服务器的变化,因此,所有车型将列出阅读:真正的在他们changedAttributes

keep in mind that: after this ajax call is complete you might want to fetch your collection again to update it (otherwise the models in your collection will don't know about the changes yo u did to the server, so all models will list read: true in their changedAttributes.

这篇关于如何更新一组使用Backbone.js的&放记录; Rails的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆