0

所以,我有一个带有 Ember.js 的简单页面,我正在尝试从 rest api 加载数据。

api由apiary.io提供,返回如下简单数据

{"semantics": [
 {key : "11111", name : "Block 1"},
 {key : "22222", name : "Block 2"},
 {key : "33333", name : "Block 3"},
 {key : "44444", name : "Block 4"},
 {key : "55555", name : "Block 5"}
]}

问题是通过 App.Semantic.find() 获取数据的模型,它没有将项目放入模板。

但是如果我在路由中设置数组值,它会按预期工作

App.SemanticRoute = Ember.Route.extend({
    model: function () {
        //return App.Semantic.find();
        return [{key:111,name:"aaaa"},{key:222,name:"qqqqq"},
                {key:3333,name:"bbbb"},key:555,name:"ccc"}];
    }
});

哪里有问题?

API 的链接 - http://bug0r.apiary.io/api/semantics

带有代码的 jsfiddle - jsfiddle

完整代码在这里

var App = window.App = Ember.Application.create({
    VERSION: '1.0',
    rootElement: '#appRoot',
    LOG_TRANSITIONS: true
});
App.deferReadiness(); // do not initialize it until we are ready

App.Adapter = DS.RESTAdapter.extend({
    namespace: 'api',
    url: 'http://bug0r.apiary.io'
});
App.Adapter.map('Semantic', {
    primaryKey: 'key'
});
App.Store = DS.Store.extend({
    revision: 12,
    adapter: App.Adapter
});

App.Router.map(function () {
    // each call will create Ember.Route instance for customizing route
    this.resource("semantic", {
        path: "/"
    });
    this.route("about", {
        path: "/about"
    });
});

/* Symantic model*/
App.Semantic = DS.Model.extend({
    key: DS.attr('string'),
    name: DS.attr('string'),
});

App.SemanticRoute = Ember.Route.extend({
    model: function () {
        return App.Semantic.find();
    }
});

App.advanceReadiness(); // ready for initialization

编辑: 1. JSON api 已修复,但仍无法正常工作。

4

1 回答 1

1

API 返回无效的 JSON(键值不应该用=by分隔:,属性应该用引号括起来)

{ "semantics": [
{key = "11111", name = "Block 1"},
{key = "22222", name = "Block 2"},
{key = "33333", name = "Block 3"},
{key = "44444", name = "Block 4"},
{key = "55555", name = "Block 5"}
] }

应该

{ "semantics": [
{"key":"11111", "name":"Block 1"},
{"key":"22222", "name":"Block 2"},
{"key":"33333", "name":"Block 3"},
{"key":"44444", "name":"Block 4"},
{"key":"55555","name":"Block 5"}
] }
于 2013-05-06T08:34:35.633 回答