简体   繁体   English

在JSON对象数组中将对象作为具有匹配键的ember-model返回

[英]Return object as ember-model with matching key in array of JSON objects

What I'm trying to accomplish is to return only one object to the model and loop through it's properties in a handlebars template. 我要完成的任务是仅将一个对象返回模型,并在车把模板中遍历其属性。 Thanx-a-Lot for any help! 感谢您的帮助!

My response looks like this: 我的回答如下:

{"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"}
}

I tried everything. 我尝试了一切。 The most obvious being these two, just as a tryout, but the ember inspector shows no model is set: 最明显的是这两个,就像试用一样,但ember检查器显示未设置任何模型:

    var sinograms = Ember.$.getJSON(apiurl);
    return sinograms['U+554A'];

I also tried: 我也尝试过:

    var sinograms = Ember.$.getJSON(apiurl);
    return sinograms[0];

NB: I can change the response format if necessary. 注意:如有必要,我可以更改回复格式。 I know it works when I loose the object-keys (without "U+554A"), but then how do I select the matching character. 我知道当我松开对象键(不带“ U + 554A”)时它可以工作,但是那我该如何选择匹配的字符。

@abuani: Upon your request. @abuani:应您的要求。 Thanx btw. 谢谢。

//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>

I can't add a comment to your initial question, but can you please post a few other pieces of code: 我无法在您的初始问题中添加评论,但是可以请您发布其他一些代码:

The router that's loading this(I imagine this is being done in your model function), the setupController if you have one, and the template that's trying to render the object. 正在加载此内容的路由器(我想这是在您的模型函数中完成的),setupController(如果有的话)以及正在尝试呈现该对象的模板。 Without this information, there's little I can do to help. 没有这些信息,我无能为力。

EDIT Since there's code: 编辑因为有代码:

I should have noticed, your model is return an objects when Ember expects the model to be an array of objects. 我应该注意到,当Ember期望模型是对象数组时,您的模型将返回一个对象。 The first thing you should do is make the return from the server an Array. 您应该做的第一件事是使服务器返回的数组为Array。 If you don't have control over this, then you can change your model to return 如果您对此无权控制,则可以更改模型以返回

return Ember.A([sinograms]);

And that should work. 那应该起作用。

After that though, you can remove the nested object within each object. 不过,此后,您可以删除每个对象内的嵌套对象。 If you don't remove that, then you also need to make the inner objects an Array so you can iterate over it. 如果不删除它,那么还需要将内部对象设置为Array,以便可以对其进行迭代。

Let me know how this goes. 让我知道这是怎么回事。

Here's the JSBIN 这是JSBIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM