Fetch 与 Reset 如何正确使用 Backbone.js
我有 4 个集合,应该填充来自服务器的数据!
我的 json 是嵌套的:
[ "data1":[...], "data2": [...], "data3": [...], "data4": [...] ]
我需要为每个集合提供不同的数据。
我开始使用 Fetch,但它为每个 Collections 获取了 整个json之前选择真正需要的数据。
当我有时使用重置时,集合中充满了一些数据 次不是!
var pack = []; var Coll1 = new ClefsButtonsCollection(); var Coll2 = new AccidentalsButtonsCollection(); var Coll3 = new NotesButtonsCollection(); var Coll4 = new RestsButtonsCollection(); $.getJSON("/api/data.json", 函数(数据){ Coll1.reset( data.data1 ); Coll2.reset( data.data2 ); Coll3.reset( data.data3 ); Coll4.reset( data.data4 ); }); pack.push( Coll1, Coll2, Coll3, Coll4 );
如何在 Dom 加载之前填充我的收藏? 我使用 require.js
I have 4 Collections which should be filled with Data from the Server!
My json is a nested one :
[ "data1":[...], "data2": [...], "data3": [...], "data4": [...] ]
and i need a for every Collections a different data.
I started to use Fetch, but it fetched for every Collections the
whole json before to choose the real needed data.When i use reset some times Collections are filled with Data some
times not!var pack = []; var Coll1 = new ClefsButtonsCollection(); var Coll2 = new AccidentalsButtonsCollection(); var Coll3 = new NotesButtonsCollection(); var Coll4 = new RestsButtonsCollection(); $.getJSON("/api/data.json", function(data){ Coll1.reset( data.data1 ); Coll2.reset( data.data2 ); Coll3.reset( data.data3 ); Coll4.reset( data.data4 ); }); pack.push( Coll1, Coll2, Coll3, Coll4 );
How can i filled my Collection before the Dom loaded?
I use require.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答是,你不能。由于您必须处理 JavaScript 的异步性质,因此您无法确定一个回调是在另一个回调之前进行的。我看到的唯一方法是不调用 api,而是直接在 html 页面中呈现 JSON 结果,如 主干常见问题解答。
The short answer is, you can't. As you have to handle with the asynchronies nature of JavaScript you cant be sure that one callback was made before an other. The only way I see is to not call the api but render the JSON result directly in your html page, as described in backbone FAQ.