2012-10-24 32 views
5

Tôi đang cố gắng tải mô hình Người dùng dữ liệu từ một tuyến đường hơn là có thể chấp nhận tên người dùng hoặc id.Sử dụng slug thay cho id

Tôi đã sửa đổi phương thức tìm RESTAdapter để chuyển id từ phản hồi JSON thành didFindRecord, nhưng không có gì được chuyển cho chế độ xem của tôi. Có vẻ như Ember vẫn đang cố sử dụng tên người dùng làm id của mô hình.

router của tôi trông như thế này:

App.Router = Em.Router.extend({ 
    location: 'history' 
, root: Em.Route.extend({ 
    user: Em.Route.extend({ 
     route: '/users/:slug' 
    , connectOutlets: function(router, context) { 
     router.get('applicationController').connectOutlet('user', context) 
     } 

    , serialize: function(router, context) { 
     return { slug: context.get('username') || context.get('id') } 
     } 

    , deserialize: function(router, params) { 
     return App.User.find(params.slug) 
     } 
    }) 
    }) 
}) 

Và phương pháp tìm tôi trông như thế này:

var CustomAdapter = DS.RESTAdapter.extend({ 
    find: function(store, type, id) { 
    var root = this.rootForType(type) 

    this.ajax(this.buildURL(root, id), 'GET', { 
     success: function(json) { 
     this.didFindRecord(store, type, json, json[root].id) 
     } 
    }) 
    } 
}) 

App.store = DS.Store.create({ 
    revision: 6 
, adapter: CustomAdapter.create({ namespace: 'api' }) 
}) 
+0

là yêu cầu ajax thực sự bị sa thải ? (Bạn có lấy lại dữ liệu không?) có thể là một vấn đề với việc thay đổi tuyên truyền –

+0

Hãy thử thực hiện việc này: Ember.run (this, function() { this.didFindRecord (lưu trữ, gõ, json, json [root] .id); }); –

Trả lời

Các vấn đề liên quan