我与导轨API一个Backbone.js的应用程序,它提供了JSON工作,如下所示:Backbone.js的集合网址
[{
"title": "Da vinci Code",
"price": "50"
},
{
"title": "Some other name",
"price": "45"
}],
TODO:
- 当用户访问的URL“www.backboneapp.com/authors/1“,它应该显示属于作者的书籍列表。
进展: - 服务器侧导轨[authorscontroller#显示]
def show
@author = Author.find(params[:id])
@books = @author.books
respond_to do |format|
format.json { render :json => @books }
format.html
end
end
2.Client端,Backbone.js的
路由器:
routes: {
'authors/:id': 'showauthor'
},
showauthor: function(id) {
$('#container').empty();
window.available_books = new AvailableBookList({
id: id
});
this.availablebooklistview = new AvailableBookListView({
id: id,
collection: available_books
});
$('#container').append(this.availablebooklistview.render().el);
}
AvailableBookList Collection:
AvailableBookList = Backbone.Collection.extend({
model: AvailableBook,
url: "/authors/" + this.id
})
视图部分:
$(document).ready(function(){
AvailableBookListView = Backbone.View.extend({
tagName: 'section',
className: 'availablebooklist',
initialize: function() {
_.bindAll(this, 'render');
this.collection.bind('reset', this.render);
},
render: function() {
this.collection.each(function(available_book) {
var view = new AvailableBookView({
model: available_book,
});
this.$('.availablebooklist').append(view.render().el);
});
return this;
}
});
});
问题:当我访问 “www.backboneapp.com/authors/1”我得到404错误。 在CONSOLE.LOG它显示: GET:www.backboneapp.com/authors/ 不确定
在Rails日志:parameters {id => undefined}
* 我在哪里去了? *
任何帮助和建议高度赞赏
在'初始化:功能(模型,期权){this.id = options.id);}'我得到**无法读取属性“身份证'未定义**,即使author_id解决方案也给了我相同的错误..'initialize:function(options){this.id = options.id; }',这个工程....但我仍然得到同样的错误**无法读取属性'ID'的undefined ** – railerhelper
我正在使用backbone.js0.5.3 – railerhelper
完成...添加视图代码.... – railerhelper