Ember检索最后一个插入ID

时间:2013-12-06 15:47:53

标签: ember.js ember-data

在我看到的每个例子中,我们总是使用数据,就像我们知道下一个ID是什么。我正在构建一个多个用户将同时使用的Web应用程序。当用户单击“创建类别”按钮时,我创建一个新记录,将其发送到我的API,将其保存在数据库中。不幸的是,Ember不知道我的新分类ID。让我们说他写了一个错字。他点击“修改”。应用程序不知道ID,因此无法完成路径:product_id / edit。

我看到两个解决方案:

  1. 我试过的一件事,可能会解决我的问题,就是在标题中返回ID。所以我会发送我的数据,如果一切顺利,返回状态201(创建),然后获取标题的内容并分配创建的ID。在纸上看起来不错,但我不知道如何用Ember获取我的标题内容。

    App.CategoriesAddRoute = Ember.Route.extend({
        setupController: function(controller) {
            controller.newRecord(); 
        },
        actions: {
           save: function() {
                controller = this.controllerFor("category"); 
                // Here I'd like to use something like .then( getHeaderContent('id') ) 
                // and assign that value to the category                
                controller.get('content').save() 
                this.transitionTo('categories.index');
            }
        },
    });
    
  2. 使用新查询重新加载我的类别数据。我想避免这种情况,但如果我别无选择,我会选择这个。

1 个答案:

答案 0 :(得分:1)

保存新模型(POST)的标准做法是使用新ID从服务器返回模型。在服务器返回模型后,Ember Data将更新模型,因此模型客户端应具有id。