Backbone.js和本地存储。 必须指定“url”属性或函数

我正在提高我对Backbone.js的了解,并从教程中获取此代码示例。 ( http://bardevblog.wordpress.com/2012/01/16/understanding-backbone-js-simple-example/ )

此示例暂时不会访问服务器,因此为了模拟从服务器检索数据,我有一个文件名movies.json。

我想做什么:

  • 在本地存储中添加json数据(使用localStorage适配器)
  • 为此,我使用Backbone.ajaxSync,localStorage适配器为别名Backbone.sync提供:我创建了方法refreshFromServer()来执行此操作
  • 这样做的原因是我试图实现一种只获取一次数据的方法(并且只在需要时刷新)

我的问题:当我调用refreshFromServer()时,我遇到错误“ Uncaught Error:’url’属性或函数必须指定 ”。 我不明白为什么因为我设置了url集合。 (url:“scripts / data / movies.json”)

示例代码

var Theater = { Models : {}, Collections : {}, Views : {}, Templates : {} } Theater.Models.Movie = Backbone.Model.extend({}) Theater.Collections.Movies = Backbone.Collection.extend({ model : Theater.Models.Movie, localStorage : new Backbone.LocalStorage("MovieStore"), // Unique name within your app. url : "scripts/data/movies.json", refreshFromServer : function() { return Backbone.ajaxSync.apply(this, arguments); }, initialize : function() { console.log("Movies initialize") } }); Theater.Templates.movies = _.template($("#tmplt-Movies").html()) Theater.Views.Movies = Backbone.View.extend({ el : $("#mainContainer"), template : Theater.Templates.movies, initialize : function() { this.collection.bind("reset", this.render, this); }, render : function() { console.log("render") console.log(this.collection.length); } }) Theater.Router = Backbone.Router.extend({ routes : { "" : "defaultRoute" }, defaultRoute : function() { console.log("defaultRoute"); Theater.movies = new Theater.Collections.Movies() new Theater.Views.Movies({ collection : Theater.movies }); Theater.movies.refreshFromServer(); //Theater.movies.fetch(); console.log(Theater.movies.length) } }) var appRouter = new Theater.Router(); Backbone.history.start(); 

笔记:

  • 如果在集合中注释localStorage属性

    Theater.Models.Movie = Backbone.Model.extend({})Theater.Collections.Movies = Backbone.Collection.extend({model:Theater.Models.Movie,// localStorage:new Backbone.LocalStorage(“MovieStore”)。 ..});

    然后在路由器调用普通的提取方法

    Theater.Router = Backbone.Router.extend({routes:{“”:“defaultRoute”},

     defaultRoute : function() { Theater.movies = new Theater.Collections.Movies() new Theater.Views.Movies({ collection : Theater.movies }); //Theater.movies.refreshFromServer(); Theater.movies.fetch(); } 

    })

我可以在我看来正确地看到json列表

  • 如果我在集合中使用localStorage属性然后调用标准的fetch()方法,我只看到一个空列表(我认为这是正常的,因为它从本地存储中读取并且为空)

  • 使用方法refreshFromServer()时,只会出现错误,使用Backbone.ajaxSync(backbone.sync的别名)

呃…我的坏。 refreshFromServer实现来自我对您之前问题的回答。 ,这是完全无用的错误。

Backbone.sync需要参数(method, model, options) ,但就目前而言,它无法从refreshFromServer获得所需的内容,因为刷新方法只是向前发送它获得的任何arguments 。 抱歉这个错误。

正确,有效的实施方案是:

 refreshFromServer : function(options) { return Backbone.ajaxSync('read', this, options); } 

它可以通过传递给options哈希的success / error回调来使用:

 this.collection.refreshFromServer({ success: function() { /* refreshed... */ }); 

或者通过jqXHR Promise API:

 this.collection.refreshFromServer().done(function() { /* refreshed... */ }) 

或者不注册回调并等待集合reset事件,如示例所示:

 this.collection.bind("reset", this.render, this); this.collection.refreshFromServer(); 

这应该工作。 如果没有,请告诉我。 我也在上一个问题中修正了我的答案,万一有人偶然发现它。

编辑:要在刷新后将数据保存到本地存储,您需要手动save每个模型:

 var collection = this.collection; collection.refreshFromServer({success: function(freshData) { collection.reset(freshData); collection.each(function(model) { model.save(); }); }});