2013-10-19 51 views
3

Ember數據只是不會將參數追加到查詢中。我有這樣Ember數據不會將參數附加到查詢中

例如

App.TagsRoute = Ember.Route.extend({ 
model: function(params) { 
    var tag = params.tag_name; 
    var entries = this.store.find('entry', {tags: tag}); 
    return entries; 
} 
}); 

一個標籤的路線,但這樣下去使得作爲this.store.find相同的請求(「入口」)。 我做錯了嗎?

編輯:

我的路由器看起來像這樣:

App.Router.map(function(){ 
this.resource('entries', function(){ 
    this.resource('entry', { path: '/entry/:entry_id/:entry_title' }); 
}); 
this.route('tags', { path: '/t/:tag_name' }); 
}); 

當我請求(例如)本地主機:8888 /#/噸/標籤
params.tag_name的值是'標籤'

EDIT2:

我的REST接口

App.ApplicationAdapter = DS.RESTAdapter.extend({ 
    bulkCommit: false, 
    buildURL: function(record, suffix) { 
     var s = this._super(record, suffix); 
     return s + ".json"; 
    }, 
    findQuery: function(store, type, query) { 
     var url = this.buildURL(type.typeKey), 
     proc = 'GET', 
     obj = { data: query }, 
     theFinalQuery = url + "?" + $.param(query); 
     console.log(url); // this is the base url 
     console.log(proc); // this is the procedure 
     console.log(obj); // an object sent down to attach to the ajax request 
     console.log(theFinalQuery); // what the query looks like 
     // use the default rest adapter implementation 
     return this._super(store, type, query); 
    } 
}); 

EDIT3:

作出一些改變,以我的TagsRoute對象我得到一個輸出:

App.TagsRoute = Ember.Route.extend({ 
model: function(params) { 
    var tag = params.tag_name; 
    var query = {tags: tag}; 
    console.log(query); 
    var entries = this.store.find('entry', query); 
    return entries; 
} 
}); 


控制檯輸出,當我要求本地主機: 8888 /#/ t/tag

對象{標籤: 「標籤」}
(主機URL)+ API/V1/entries.json
GET
對象{數據:對象}
(主機URL)+ API/V1/entries.json?標記=標記
類{type:function,query:Object,store:Class,isLoaded:true,meta:Object ...}

Ember數據附加GET參數。我想我的錯誤,也許是請求的網址,它應該是這樣的
(主機URL)+ API/V1 /標籤/:tag_name.json
而不是
(主機URL)+ API/V1 /entries.json?tags=:tag_name

SOLUTION

餘燼數據(餘燼數據1.0.0-beta.3-16-g2205566)的構建被打破。當我將腳本src更改爲builds.emberjs.com.s3.amazonaws.com/canary/daily/20131018/ember-data.js時,一切都很完美。
適當的方法來添加GET參數是:

var query = {param: value}; 
var array = this.store.find('model', query); 

感謝您的幫助

+0

發送的請求是什麼? – Kingpin2k

+0

我編輯了問題@daniel – cmszc

回答

1

你正在做的一切正確,你確定請求被髮回到服務器不包含查詢?

完整的查詢不會在JQuery進行調用之前創建。

您是否看過Chrome中的網絡標籤(或者您正在使用的任何瀏覽器)以查看它發回的內容。

關注以下jsbin控制檯,它顯示了當您使用查找發生與對象(查詢):

App.MyAdapter = DS.RESTAdapter.extend({ 
findQuery: function(store, type, query) { 
    var url = this.buildURL(type.typeKey), 
     proc = 'GET', 
     obj = { data: query }, 
     theFinalQuery = url + "?" + $.param(query); 
    console.log(url); // this is the base url 
    console.log(proc); // this is the procedure 
    console.log(obj); // an object sent down to attach to the ajax request 
    console.log(theFinalQuery); // what the query looks like 
    // use the default rest adapter implementation 
    return this._super(store, type, query); 
    }, 
}); 

http://emberjs.jsbin.com/AyaVakE/1/edit

其他問題:

擊球:http://localhost:8888/#/t/tag激發標籤路由,發送'tag'到tag_name。你的模型鉤子是正確的。你在哪裏看到請求是一樣的?

此外,您提到了store.find('entries)和store.find('entry')。我知道他們處理大多數事情的多元化,但是你應該確保那些最終成爲相同的端點/ api /條目。

+0

感謝您的回答@丹尼爾! 條目和條目資源指向api /條目。 如果我請求(例如)http:// localhost:8888 /#/ t/tag,則api使用param'tag'過濾響應。 在這種情況下,我必須指向api/tags/tag.json(使用param ='tag')。 chrome中的網絡選項卡向我顯示與this.store.find('entry')相同的請求。沒有附加GET參數,這就是問題所在。 (我看到了這個請求) – cmszc

+0

如果你添加和使用上面顯示的適配器,並改用它,它在控制檯中輸出了什麼內容? – Kingpin2k

+0

你使用的是什麼版本的燼數據? – Kingpin2k

0

RESTAdapter通過黏合它到data屬性的設置對象發送您的查詢對象的jQuery.ajax()方法。 (請參閱here瞭解有關設置對象完成情況的信息。)

看起來可能標籤名稱未定義。嘗試確保tag_name參數正確來自您的路線。

+0

我用一些代碼編輯了我的問題,謝謝! – cmszc