我與導軌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