骨干网 - 从API获取JSON数据 [英] Backbone - Get JSON Data from API
问题描述
我在玩弄骨干这最后的日子..
I'm playing around with Backbone these last days..
我想从Twitter搜索API接收的一些数据。但我真的不明白它是如何工作的。这是我的code:
I want to receive some data from the twitter search API. But I don't really understand how it works. This is my code:
(function($){
Tweet = Backbone.Model.extend();
Tweets = Backbone.Collection.extend(
{
model: Tweet,
url: 'http://search.twitter.com/search.json?q=Hamburg&rpp=5&lang=all&callback=?',
parse: function(response)
{
return response.results;
}
});
DefaultTweetView = Backbone.View.extend(
{
initialize: function(){
_.bindAll(this, 'render');
}
template: _.template('<p>@<%= from_user %> <em></em></p><p><%= text %></p><p><%= location %></p>'),
render: function()
{
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
app = new Tweet();
})(jQuery);
我不认为这可能是正确的。但我不知道如何处理它:(有人能帮助我,或张贴一个链接,我可以按照有关JSON数据和骨干好吗?
I don't think this could be right. But I don't know how to handle it :( Could someone help me or post a link where I could follow some instructions about json data and backbone please?
推荐答案
您的实际code不起作用,因为你有一个语法错误,而你只是初始化模式。
Expications
Your actual code doesn't work because you have a syntax error, and you just initialized the model.
您不能访问Twitter的API不覆盖Backbone.sync法(同源策略的问题:<一href=\"http://en.wikipedia.org/wiki/Same_origin_policy\">http://en.wikipedia.org/wiki/Same_origin_policy,该解决方案已经张贴在这里:<一href=\"http://stackoverflow.com/questions/8430113/backbone-collection-jsonp-ajax-results-not-generating-model-correctly\">Backbone收藏JSONP AJAX结果无法正确生成模型的)
You can't access to Twitter API without overwrite the Backbone.sync method (problem of the same origin policy : http://en.wikipedia.org/wiki/Same_origin_policy, the solution has already posted here : Backbone Collection jsonp ajax results not generating model correctly)
在我看来,学习骨干的最好的事情JS是了解如何code ++工程,并帮助你,骨干JS文件是在这里:的http://documentcloud.github.com/backbone/
In my view, the best thing to learn Backbone JS is to understand how code works, and to help you, the Backbone JS documentation is here : http://documentcloud.github.com/backbone/
我创建了一个工作的jsfiddle与code在这里: http://jsfiddle.net/Atinux/v4K6A/
I created a working jsFiddle with your code here : http://jsfiddle.net/Atinux/v4K6A/
所以,有了它,你就可以开始更好地理解骨干JS。
So with it you can begin to better understand Backbone JS.
这篇关于骨干网 - 从API获取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!