Backbone.js的和本地存储。 A" URL"必须指定属性或功能 [英] Backbone.js and local storage . A "url" property or function must be specified
问题描述
我提高我的Backbone.js的有关知识,并有从教程采取了这种code样品。 (http://bardevblog.word$p$pss.com/2012/01/16/understanding-backbone-js-simple-example/)
I'm improving my knowledge about Backbone.js and have this code sample taken from a tutorial. (http://bardevblog.wordpress.com/2012/01/16/understanding-backbone-js-simple-example/)
这个例子将无法访问服务器现在,所以从我有一个文件名movies.json服务器模拟数据的检索。
This example will not access the server for now, so to simulate the retrieval of data from the server I have a file name movies.json.
我所试图做的事:
- 在本地存储添加JSON数据(使用本地存储适配器)
- 对于这个我使用Backbone.ajaxSync,这是考虑到别名Backbone.sync由localStorage的适配器:我创建的方法refreshFromServer()来做到这一点
- 这样做的原因,这是我想要实现的方式来获取数据只有一次(也是唯一刷新,当我需要)
我的问题:
我有一个错误未捕获的错误:URL性质或功能必须指定当我打电话refreshFromServer()。
我不明白为什么,因为我设置URL集合。 (URL:脚本/数据/ movies.json)
My issues: I'm having an error "Uncaught Error: 'url' property or function must be specified" when I call refreshFromServer (). I do not understand why because I set the url collection. (url : "scripts/data/movies.json" )
样code
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的属性。
If a comment localStorage property in the collection
Theater.Models.Movie = Backbone.Model.extend({})
Theater.Collections.Movies = Backbone.Collection.extend({
型号:Theater.Models.Movie,
// localStorage的新Backbone.LocalStorage(MovieStore)
...
});
Theater.Models.Movie = Backbone.Model.extend({}) Theater.Collections.Movies = Backbone.Collection.extend({ model : Theater.Models.Movie, //localStorage : new Backbone.LocalStorage("MovieStore") ... });
然后在路由器正常通话的方法获取
and then in router call normal fetch method
Theater.Router = Backbone.Router.extend({
路线:{
:默认路由
}
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列表
I can see the json list correctly in my view
-
如果我使用localStorage的属性集合中,然后调用标准fetch()方法,我只看到一个空的列表(我想因为它是从本地存储读为空是正常的)
If I use the localStorage property in the collection and then call the standard fetch () method, I see only an empty list (I think it is normal as it is read from the local storage and is empty)
只能使用方法refreshFromServer()女巫使用Backbone.ajaxSync(别名backbone.sync)时出现的错误
The error only occurs when using the method refreshFromServer () witch use Backbone.ajaxSync (alias for backbone.sync)
推荐答案
的嗯...我的坏。在 refreshFromServer
的实施是从我的回答你的<一个href=\"http://stackoverflow.com/questions/14071203/backbone-collection-from-json-file-and-cache-on-localstorage\">earlier问题,,而且完全,白白错了。的
Err... my bad. The refreshFromServer
implementation is from my answer to your earlier question., and it's completely, uselessly wrong.
Backbone.sync
预计参数(方法,模型,期权)
,但因为它的立场,这不是'吨得到它从 refreshFromServer
需要,因为刷新方法简单地发送着什么参数
它得到。对不起,我错了。
Backbone.sync
expects arguments (method, model, options)
, but as it stands, it doesn't get what it needs from refreshFromServer
because the refresh method simply sends forward whatever arguments
it gets. Sorry for the mistake.
正确的,执行工作将是:
The correct, working implementation would be:
refreshFromServer : function(options) {
return Backbone.ajaxSync('read', this, options);
}
可以使用,也可以通过成功
/ 错误
回调传递到选项
散:
this.collection.refreshFromServer({ success: function() { /* refreshed... */ });
或通过jqXHR承诺API:
Or via the jqXHR Promise API:
this.collection.refreshFromServer().done(function() { /* refreshed... */ })
还是不报名参加回调并等待集合重置
事件就像你的例子:
this.collection.bind("reset", this.render, this);
this.collection.refreshFromServer();
这应该工作。请让我知道,如果它没有。我修好了我在previous问题答案也一样,万一有人绊倒了。
This should work. Please let me know if it doesn't. I fixed my answer in the previous question too, in case someone stumbles onto it.
编辑::要刷新你需要后数据保存到本地存储手动保存
每个模型:
To save the data to local storage after refreshing you need to manually save
each of the models:
var collection = this.collection;
collection.refreshFromServer({success: function(freshData) {
collection.reset(freshData);
collection.each(function(model) {
model.save();
});
}});
这篇关于Backbone.js的和本地存储。 A&QUOT; URL&QUOT;必须指定属性或功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!