[英]access to a related collection in an ember-model object via hasMany relationship
[英]Return object as ember-model with matching key in array of JSON objects
我要完成的任務是僅將一個對象返回模型,並在車把模板中遍歷其屬性。 感謝您的幫助!
我的回答如下:
{"U+554A":{
"id":1,
"unihex":"U+554A",
"num_reference":"呵",
"totalStrokes":10,
"kMandarin":"a\n",
"kDefinition":"exclamatory particle\n"},
"U+611B":{
"id":2,
"unihex":"U+611B",
"num_reference":"愛",
"totalStrokes":13,
"kMandarin":"\u00c3\u00a0i\n",
"kDefinition":"love, be fond of, like\n"}
}
我嘗試了一切。 最明顯的是這兩個,就像試用一樣,但ember檢查器顯示未設置任何模型:
var sinograms = Ember.$.getJSON(apiurl);
return sinograms['U+554A'];
我也嘗試過:
var sinograms = Ember.$.getJSON(apiurl);
return sinograms[0];
注意:如有必要,我可以更改回復格式。 我知道當我松開對象鍵(不帶“ U + 554A”)時它可以工作,但是那我該如何選擇匹配的字符。
@abuani:應您的要求。 謝謝。
//app.js
App = Ember.Application.create();
App.Router.map(function() {
this.resource('signup');
this.resource('login');
this.resource('profile');
this.resource('overview');
this.resource('practice');
});
App.OverviewRoute = Ember.Route.extend({
model: function() {
var url = 'http://localhost/~hiufung/RoadToChinese/index.php/api/sinograms/random?limit=2';
var sinograms = Ember.$.getJSON(url);
return sinograms;
}
});
//index.html (inline template)
<script type="text/x-handlebars" id="overview">
<header class="bar bar-nav">
<a class="icon icon-left pull-left" href="back"></a>
<h1 class="title">RoadToChinese</h1>
<a id="showRightPush" class="icon icon-gear pull-right" href="overview-settings"></a>
</header>
<div class="content">
<div class="content-padded">
{{#each object in model}}
<p>{{object.num_reference}}<p>
{{/each}}
</div>
</div>
</script>
我無法在您的初始問題中添加評論,但是可以請您發布其他一些代碼:
正在加載此內容的路由器(我想這是在您的模型函數中完成的),setupController(如果有的話)以及正在嘗試呈現該對象的模板。 沒有這些信息,我無能為力。
編輯因為有代碼:
我應該注意到,當Ember期望模型是對象數組時,您的模型將返回一個對象。 您應該做的第一件事是使服務器返回的數組為Array。 如果您對此無權控制,則可以更改模型以返回
return Ember.A([sinograms]);
那應該起作用。
不過,此后,您可以刪除每個對象內的嵌套對象。 如果不刪除它,那么還需要將內部對象設置為Array,以便可以對其進行迭代。
讓我知道這是怎么回事。
這是JSBIN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.