私はbackbone.jsアプリケーションを既存のCodeigniterAPIに接続しようとしています。githubでtodosの例を見て、そこから構築しました。'read'で呼び出されるfindAll関数をオーバーライドし、ページを取得してフェッチ関数に戻そうとしています。
findAll:function(){console.log('synch --findAll');
var url = "/folio/get/8";
var data = '';
var postmethod = 'GET';
$.ajax({
url : url,
type : postmethod,
async: false,
data: data,
success: function(response)
{
console.debug("response",response);
console.debug("response.pages", response["pages"]);
return _.values(response["pages"]);
}
});
}
APIは次のようなものを返します-console.debug( "response"、response)を介して出力します。
{
"id": "8",
"facebook_id": "123456789",
"title": "title",
"access_date": null,
"rating_avg": "0",
"pages": [
{
"id": "6",
"picture1": {
"id": "3",
"tag": "1",
"tip": "Crazy",
"facebook_picture_id": "1239102391023"
},
"picture2": "28",
"picture3": "29",
"picture4": null,
"picture5": null,
"picture6": null,
"caption1": "caption 1",
"caption2": "caption 2",
"sequence": "2",
"folio": "8",
"ts": "2012-04-10 15:13:23",
"template": "#page-template-2"
},
{
"id": "5",
"picture1": "24",
"picture2": "25",
"picture3": "26",
"picture4": null,
"picture5": null,
"picture6": null,
"caption1": "caption 1",
"caption2": "caption 2",
"sequence": "4",
"folio": "8",
"ts": "2012-04-10 15:13:23",
"template": "#page-template-2"
}
]
}
ただし、console.debug( "response.pages"、response ["pages"])は「undefined」を出力します。なぜそれをするのですか?
よろしくお願いします!
- - - - - - - - - - 編集 - - - - - - - - - - -
ご回答有難うございます。モデル内からajax呼び出しを実行できるヒントは非常に役立ちます。問題は、複数のページを1つのPageListコレクションにまとめようとしていることです。
$(function(){
// Page Collection
// ---------------
var PageList = Backbone.Collection.extend({
model: Page,
localStorage: new Store("wickes-backbone"), // this to get hold of my overwritten localstorage file - it is not actually a localStorage
nextOrder: function() {
if (!this.length) return 1;
return this.last().get('order') + 1;
},
comparator: function(page) {
return page.get('order');
}
});
window.Pages = new PageList;
});
したがって、appviewの初期化関数内で私は呼び出しています
Pages.fetch();
これにより、すべてのページにデータが入力され、ビューが更新されます。モデル自体の中でそれを行う方法が本当にわかりませんか?