Get server URL for class Ember DS.Model

When using Ember Data for my models, there are some cases where I need to bypass data restrictions and gain access to other quasi-permanent URLs on my server.

For example, I have a Feed object that records a data stream. To access the models, I have a RESTful endpoint:

 /feeds/:feed_id 

To start and stop writing a feed, I need to send a PATCH to a URL, for example:

 /feeds/:feed_id?update_action=start 

Subsequently, I can reload my model and see the changes reflected in it.

In this case, I need to access $.ajax , and the URL will be the same as the one that Ember will use. However, I cannot figure out how to extract this information from Ember.

So far, I could best:

 DS.Model.reopen rootForModel: Ember.computed( -> @.store.adapterForType(@).serializer.rootForType(@.constructor) ) pluralRootForModel: Ember.computed( -> @.store.adapterForType(@).serializer.pluralize(@get("rootForModel")) ) 

So for the instance of App.FeedItem I can do:

 this.get("rootForModel") # feed_item this.get("pluralRootForModel") # feed_items 

And I assume that this will sync with any settings made to the adapter, etc.

Subsequently, I can name:

 $.ajax url: @get("pluralRootForModel") + "/" + @get("id") data: update_action: "start" type: "PATCH" 

Is it completely in the left margin? Is there a more direct way to compose these URLs?

Another (related question) gets an underlined name for this model.

 App.MyModelController => my_model_controller 

I did something like:

Ember.Object.reopenClass

 ###* * The underscored name for this. * ie App.MyClass -> my_class * From an instance, use this.constructor.underscored_class_name() * @return {String} This classname, underscored. ### underscored_class_name: -> _.underscored("#{@}".replace(/^.*?\./g, "")) 

This is madness? Are there any better ways?

+7
ember-data
source share
1 answer

Check buildURL in DS.RESTAdapter .

If you want to use underscores on server paths and keys, check out the DS.ActiveModelAdapter (and its default serializer, DS.ActiveModelSerializer ). This adapter has its own implementation of buildURL .

+3
source share

All Articles