backbone.jsアプリケーションを既存のCodeigniter APIに接続しようとしています。私はgithubのtodosの例を見てそこから構築しました。私は「読み取り」に呼び出され、findAllの機能をオーバーライドすることだし、ページを取得し、バックフェッチ機能にそれらを返すようにしようとしています :backbone.jsのsync関数をオーバーライドするときのサーバーレスポンスの処理方法
のfindAll:機能(){ はconsole.log( '同期 - 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( "レスポンス"、応答)を介して出力を:
{
"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コレクションにいくつかのページを取得しようとしていますことをされていますので、APPVIEWのintitialize関数内で、私はすべてのページを移入してビューを更新し
Pages.fetch();
を呼び出しています
$(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;
});
。モデル自体の中でそれをどうやって行うのか本当にわからないのですか?
あなたのビュー/モデルを投稿できますか? –
をコードに変更すると、 のconsole.debug( "response.pages"、response.pages); コンソール版画:未定義 response.pages そう変わらない結果あなたの答えのための – Dine