2013-09-01 71 views
2

我一直在使用燼模型創建FIXTURES,但我不能夠使用以下節點模板如何在ember-model中使用belongsTo?

"logged_in": { 
     "id" : "1", 
     "logged": true, 
     "username": "sac1245", 
     "account_id": "4214" 
    } 

我已經實現belongsTo關係這個節點,但它拋出一個錯誤:Uncaught Error: Ember.Adapter must implement findQuery
這裏我列出我的模型代碼:

Astcart.Application = Ember.Model.extend({ 
    name: Ember.attr(), 
    logo_url : Ember.attr(), 
    list: Ember.hasMany('Astcart.List', {key: 'list', embedded: true}), 
    logged_in: Ember.belongsTo('Astcart.Logged_in', {key: 'logged_in'}) 
}); 

    Astcart.List = Ember.Model.extend({ 
     name: Ember.attr() 
    }); 

    Astcart.Logged_in = Ember.Model.extend({ 
     id: Ember.attr(), 
     logged: Ember.attr(), 
     username: Ember.attr(), 
     account_id: Ember.attr() 
    }); 

    Astcart.Application.adapter = Ember.FixtureAdapter.create(); 

    Astcart.Application.FIXTURES = [ 
     { 
      "id"  : "1", 
      "logo_url": "img/logo.jpg", 
      "logged_in": { 
       "id" : "1", 
       "logged": true, 
       "username": "sac1245", 
       "account_id": "4214" 
      },   
      "name": "gau", 
      "list": [ 
         { 
          "id"  : "1", 
          "name": "amit" 
         }, 
         { 
          "id"  : "2",     
          "name": "amit1" 
         } 
      ] 
     } 
    ]; 

模板代碼:

{{#each item in model}} 
     {{item.name}} 
     {{item.logged_in.logged}}        
    {{/each}} 

路由器代碼:

Astcart.ApplicationRoute = Ember.Route.extend({ 
    model: function() { 
     return Astcart.Application.find(); 
    } 
    }); 

任何一個可以告訴我如何訪問模板上面節點的數據?

回答

3

您當前的燈具都嵌入logged_in數據:

Astcart.Application.FIXTURES=[ 
    { 
     ... 
     "logged_in": { 
      "id": "1", 
      "logged": true, 
      "username": "sac1245", 
      "account_id": "4214" 
     }, 
     ... 
    } 
]; 

所以logged_in屬性映射需要的參數embedded: true,才能夠找到這些數據,如下:

Astcart.Application = Ember.Model.extend({ 
    ... 
    logged_in: Ember.belongsTo('Astcart.Logged_in', {key: 'logged_in', embedded: true }) 
}); 

沒有這個它會嘗試查詢Astcart.Logged_in

Astcart.Logged_in.find(1) 

因爲它沒有一個適配器,將失敗,異常:Uncaught Error: Ember.Adapter must implement findQuery

你可以看到在撥弄該工作http://jsfiddle.net/marciojunior/8zjsh/

相關問題