2012-05-23 50 views
3

問題:從服務器接收到JSON,但只有id屬性具有值。 GUI僅顯示字符串「2 1」,並且忽略{{photoName}}。手動調用MyApp.PhotoController.get('content').objectAt(0).get('photoName')返回undefined,而MyApp.PhotoController.get('content').objectAt(0).get('id')返回正確的ID。Ember僅填充「id」屬性的數據

任何建議?

//我的模型:

MyApp.Photo = DS.Model.extend({ 
    id: DS.attr('number'), 
    photoName: DS.attr('string'), 
    photoDescription: DS.attr('string'), 
    photoFullSizeURL: DS.attr('string'), 
    photoThumbnailURL: DS.attr('string') 
}); 

MyApp.Photo.reopenClass({ 
    url: 'photos.json' 
}); 

//我StateManager

MyApp.stateManager = Ember.StateManager.create({ 
    rootElement: '#mainArea', 
    initialState: 'showMainView', 

    showMainView: Ember.ViewState.create({ 
     enter: function(stateManager) { 
      this._super(stateManager); 
      var photos = MyApp.store.findAll(MyApp.Photo); 
      MyApp.PhotosController.set('content', photos); 
     }, 

     view: Em.ContainerView.create({ 
      childViews: ['photoListView'], 

      photoListView: Em.View.extend({ 
       elementId: 'photoList', 
       templateName: 'photo-list-view', 
       contentBinding: 'MyApp.PhotosController.content' 
      }) 
     }) 
    }) 
}) 

//我的控制器:

MyApp.PhotosController = Ember.ArrayProxy.create({ 
    content: [] 
}); 

//我的模板:

<script type="text/x-handlebars" data-template-name="photo-list-view"> 
     PHOTOS:<br/> 
     {{#each content}} 
      {{photoName}} {{id}} 
     {{/each}} 
</script> 

// JSON從服務器接收:

[ 
    { 
     "id": 2, 
     "photoName": "Bird Photo", 
     "photoDescription": "Bird Photo Description", 
     "photoFullSizeUrl": "photos/bird.jpg", 
     "photoThumbnailUrl": "photos/bird_thumb.png"   
    }, 
    { 
     "id": 1, 
     "photoName": "Bird Photo 2", 
     "photoDescription": "Bird Photo Description 2", 
     "photoFullSizeUrl": "photos/bird.jpg", 
     "photoThumbnailUrl": "photos/bird_thumb.png" 
    } 
] 

的代碼還張貼作爲一個要點在這裏:https://gist.github.com/2775283

回答

1

啊..明白了..需要添加代碼去camelize我的屬性:

d https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md

DS.Model.reopen({ 
    namingConvention: { 
    keyToJSONKey: function(key) { 
     return key; 
    }, 

    foreignKey: function(key) { 
     return key; 
    } 
    } 
}); 

從以下頁面,我想我應該讀,反正發現這個