覆盖buildURL方法以包含父模型的id [英] Override buildURL method to include parent model's id

查看:605
本文介绍了覆盖buildURL方法以包含父模型的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于ember数据和子类化DS.RESTAdapter来覆盖 buildURL



我有两个端点。让我们说他们是:

  example.com/users/user-id 
example.com/users/user- id / images

将user-id替换为实际的用户ID。



所以我有两个模型:

  App.User = DS.Model.extend 
图像:DS.hasMany'image',
async:true
inverse:'user'

App.Image = DS.Model.extend
用户: DS.belongsTo'user',
async:true
inverse:'images'

但是,来自 / users / user-id 端点的有效载荷不会返回图像ID。我不能控制(这是第三方api)。



因此,我需要单独请求/ users / user-id / images。我将DS.RESTAdapter子类化为指定url:

  App.ImageAdapter = DS.RESTAdapter.extend 
buildURL:(type,id) - >
#需要返回/ users / user-id / images
#但需要获取user-id的值

从路由中,我甚至会将用户ID传递给商店,以便它可以调用 buildURL 方法在自定义适配器与用户ID?

  App.UserRoute = Em.Route.extend 
afterModel: (型号) - >
#如何提供`model.get'id'的值存储/ adapter / buildURL?
#我甚至在这里做吗?
promise = @get('store')。find'image'
@controllerFor('images')。set'model',promise

我想我可以提供一个散列到 find 并跳过 buildURL 但是 findQuery 是一种私有的方法。有更好的方法吗?

  App.UserRoute = Em.Route.extend 
afterModel :(模型) - >
promise = @get('store')。find'image',userId:model.get'id'
@controllerFor('images')。set'model',promise

App.ImageAdapter = DS.RESTAdapter.extend
findQuery:(store,type,query) - >
imagesURL =#{@host}/users/#{query.userId}/images
delete query.userId
@ajax(imageURL,'GET',{data:query} );


解决方案

覆盖<$ c $绝对没有错c> findQuery ,这是扩展适配器的乐趣的一部分。我们不得不做同样的事情(我们的休息终点真的令人兴奋)。


Regarding ember-data and subclassing DS.RESTAdapter to override buildURL.

I have two endpoints. Lets say they are:

example.com/users/user-id
example.com/users/user-id/images

Replace "user-id" with the actual user id.

So I have two models:

App.User = DS.Model.extend
    images: DS.hasMany 'image',
        async: true
        inverse: 'user'

App.Image = DS.Model.extend
    user: DS.belongsTo 'user',
        async: true
        inverse: 'images'

However, the payload from /users/user-id endpoints do not return the image ids. I do not have control over this (it's a third party api).

Therefore I need to make a separate request to /users/user-id/images. I'm subclassing DS.RESTAdapter in order to specify the url:

App.ImageAdapter = DS.RESTAdapter.extend
    buildURL: (type, id) ->
        # need to return "/users/user-id/images"
        # but need to get the value of user-id somehow

From the route, how would I even go about passing in the user id to the store so that it can call the buildURL method in the custom adapter with the user id?

App.UserRoute = Em.Route.extend
    afterModel: (model) ->
        # how to provide value of `model.get 'id'` to store/adapter/buildURL?
        # do I even do that here?
        promise = @get('store').find 'image'
        @controllerFor('images').set 'model', promise

I guess I can provide a hash to find and skip buildURL altogether. But findQuery is a private method. Is there a more preferred way to do this?

App.UserRoute = Em.Route.extend
    afterModel: (model) ->
        promise = @get('store').find 'image', userId: model.get 'id'
        @controllerFor('images').set 'model', promise

App.ImageAdapter = DS.RESTAdapter.extend
    findQuery: (store, type, query) ->
        imagesURL = "#{@host}/users/#{query.userId}/images"
        delete query.userId
        @ajax(imageURL, 'GET', { data: query });

解决方案

There is definitely nothing wrong with overriding findQuery, that's part of the fun of being able to extend the adapters. We've had to do the same thing (our rest endpoints are really exciting).

这篇关于覆盖buildURL方法以包含父模型的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆