"如何"保存整个集合在Backbone.js的 - Backbone.sync或jQuery.ajax? [英] "How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?

查看:108
本文介绍了"如何"保存整个集合在Backbone.js的 - Backbone.sync或jQuery.ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我清楚地知道这是可以做到,我已经看了不少地方(包括:的保存整个集合最佳实践?)。但我还是不太清楚究竟是如何被它写成code? (该文章解释它的英文这将会是巨大的,有一个JavaScript具体的解释:。)

I am well aware it can be done and I've looked at quite a few places (including: Best practice for saving an entire collection?). But I'm still not clear "exactly how" is it written in code? (the post explains it in English. It'd be great to have a javascript specific explanation :)

说我有模式的集合 - 模型本身可能有嵌套的集合。我已经覆盖父集合的的toJSON()方法,我得到一个有效的JSON对象。我想拯救整个集合(对应JSON),但骨干网似乎不进来,建有该功能。

Say I have a collection of models - the models themselves may have nested collections. I have overridden the toJSON() method of the parent collection and I am getting a valid JSON object. I wish to "save" the entire collection (corresponding JSON), but backbone doesn't seem to come in-built with that functionality.

var MyCollection = Backbone.Collection.extend({
model:MyModel,

//something to save?
save: function() {
   //what to write here?
 }

});

我知道的地方你不得不说:

I know somewhere you have to say:

Backbone.sync = function(method, model, options){
/*
 * What goes in here?? If at all anything needs to be done?
 * Where to declare this in the program? And how is it called?
 */
}

在视图与处理它负责告诉收藏拯救自己的服务器上做(能够处理批量更新/创建请求)。

Once the 'view' is done with the processing it is responsible for telling the collection to "save" itself on the server (capable of handling a bulk update/create request).

出现的问题:

  1. 如何/在code写什么线它一起?
  2. 什么是回调的正确的位置,以及如何指定成功/错误回调?我的意思是语法?我不清楚骨干注册回调的语法...

如果这确实是一个棘手的工作,然后我们可以调用jQuery.ajax一个视图中,并通过 this.successMethod this.errorMethod 作为成功/错误回调?将它的工作?

If it is indeed a tricky job then can we call jQuery.ajax within a view and pass the this.successMethod or this.errorMethod as success/error callbacks?? Will it work?

我需要同步的思想骨干的方式 - 我知道我肯定失去了一些东西WRT,同步整个集合

I need to get in sync with backbone's way of thinking - I know I'm definitely missing something w.r.t., syncing of entire collections.

推荐答案

我立即想到的不是凌驾于保存方法上Backbone.Collection的方法,但包装到另外Backbone.Model收集并覆盖上了的toJSON方法。然后Backbone.js的会将该模式作为一个单一的资源,你没有破解的backone办法想得太多了。

My immediate thought is not to override the method on save method on Backbone.Collection but wrap the collection in another Backbone.Model and override the toJSON method on that. Then Backbone.js will treat the model as a single resource and you don't have to hack the way backone thinks too much.

需要注意的是Backbone.Collection拥有的toJSON方法,所以大部分的工作是为你做。你只需要代理的包装Backbone.Model到Backbone.collection的的toJSON方法。

Note that Backbone.Collection has a toJSON method so most of your work is done for you. You just have to proxy the toJSON method of your wrapper Backbone.Model to the Backbone.collection.

var MyCollectionWrapper = Backbone.Model.extend({
url: "/bulkupload",

//something to save?
toJSON: function() {
    return this.model.toJSON(); // where model is the collection class YOU defined above
 }

});

这篇关于"如何"保存整个集合在Backbone.js的 - Backbone.sync或jQuery.ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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