如何使用带有ember-data的自定义URL?

时间:2013-09-18 14:52:18

标签: javascript rest ember.js ember-data

如何将自定义网址与ember-data一起使用?例如:

PUT /users/:user_id/deactivate
PUT /tasks/:task_id/complete
PUT /jobs/1/hold

2 个答案:

答案 0 :(得分:1)

现在似乎没有办法用ember-data做到这一点。但是,回到ajax然后使用它是微不足道的。例如:

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 :(得分:0)

您可以在Ember路由器中定义相当复杂的URL。

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'});
  });
});

这给了我们:

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

Ember决定是使用GET,POST,PUT还是DELETE,具体取决于它是从服务器获取,持久保存新资源,更新现有资源还是删除它。

请参阅here了解基本但功能正常的博客类型应用,其中包含可以保留的帖子和评论,或here用于从第三方服务器获取资源的更复杂的应用。