How to use custom urls with ember data data?

How can I use custom urls with ember-data data? For instance:

PUT /users/:user_id/deactivate
PUT /tasks/:task_id/complete
PUT /jobs/1/hold
+4
source share
2 answers

There seems to be no way to do this right now using ember-data. However, it is trivial to just go back to ajax and use this instead. For instance:

App.UsersController = Ember.ArrayController.extend
  actions:
    deactivate: (user) ->
      # Set the user to be deactivated
      user.set('deactivated', true)

      # AJAX PUT request to deactivate the user on the server
      self = @
      $.ajax({
        url: "/users/#{user.get('id')}/deactivate"
        type: 'PUT'
      }).done(->
        # successful response. Transition somewhere else or do whatever
      ).fail (response) ->
        # something went wrong, deal with the response (response.responseJSON) and rollback any changes to the user
        user.rollback()
+1
source

You can identify fairly complex URLs from your Ember router.

App.Router.map(function() {
  this.resource('posts', function() {
    this.route('new');
  });
  this.resource('post', { path: '/posts/:post_id' }, function() {
    this.resource('comments', function() {
      this.route('new');
    });
    this.route('comment', { path: 'comments/:comment_id'});
  });
});

This gives us:

/posts
/posts/new
/posts/:post_id
/posts/:posts_id/comments
/posts/:posts_id/comments/new
/posts/:posts_id/comments/:comment_id

Ember decides whether to use GET, POST, PUT, or DELETE depending on whether it is retrieved from the server, whether a new resource is saved, an existing resource is updated, or deleted.

, , , .

0

Source: https://habr.com/ru/post/1502842/


All Articles