2013-05-06 75 views
0

所以,我有一個Ember.js簡單的頁面,我試圖從其他API加載數據。Ember.js從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 accquires數據(),它並沒有把項目模板。

但它正常工作,如果我在航線

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固定的,但還是不行。

回答

1

的API返回無效JSON(鍵值不應該由=被分隔而是通過:和屬性應該用引號括起來)

{ "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"} 
] } 
+0

我不好,肯定的! – bug0r 2013-05-06 08:43:31

+0

修復了這個,但沒有幫助 – bug0r 2013-05-06 08:44:42

+0

檢查更新的答案。根據JSON規範,JSON屬性需要用引號引起來。 – ddewaele 2013-05-06 08:57:34