Ember:动态网址中未保存的模型 [英] Ember: unsaved models in dynamic URLs

查看:87
本文介绍了Ember:动态网址中未保存的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ember应用程序中使用FixtureAdapter,但是当我切换到RESTAdapter时,我的网址不再有效。



该应用程序是一种记分类型事情,我希望用户能够记录所有的分数,而无需连接到Web。在游戏完成后,他们可以选择将所有数据保存到服务器。



现在,当Ember想要路由时,匹配/:match_id,ID isn'因为我没有提交任何服务器/商店,所以我的模型还没有一个ID,我得到的URL像:/ match / null / games / null


$ b $这是预期的行为吗?如果是,是否有解决办法?我考虑使用model.clientId,然后覆盖每个路由的模型钩子,以便在存在时尝试使用id存储模型并将其从后退到clientId。任何其他想法?



2013年3月10日更新:



为了适应我的需要,允许(现在)忘记在本地存储和REST适配器之间来回移动:

 应用程序.Store = DS.Store.extend({
revision:11,
adapter:DS.RESTAdapter.extend({
namespace:'api / v1',
bulkCommit:true ,
generateIdForRecord:function(store,record){
return'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/ [xy] / g,function(c){
var r = Math.random()* 16 | 0,v = c =='x'?r:(r& 0x3 | 0x8);
return v.toString(16);
});
}
})
});

UUID函数取自:在JavaScript中创建GUID / UUID

解决方案

如果没有提交记录,那么它不应该有一个 id 。此外,序列化查看该记录的应用程序状态的URL不会有任何意义,因为在其提交之前,记录将不会存在于另一个浏览器中。你不能把这个url粘贴到别的地方,把它放在你离开的位置。



我想你真正想做的是将应用程序的状态不同的序列化(即。当记录未提交时,生成较不具体的url)。您可以通过覆盖路由中的 serialize 方法来实现此目的。



例如,

  App.PostRoute = Ember.Route.extend({
serialize:function(model,params){
if(model& ;& model.get('isNew')){
//对应于路径'/ post /:post_id'
return {post_id:'new'}
}

return this._super(model,params);
}
});

现在,如果您调用 transitionToRoute('post',post),其中 post 是一个新创建但未提交的记录,应用程序状态将序列化到路径 / post / new 。如果你传递一个具有 id 的承诺记录,它将像往常一样被序列化。


I'm currently using the FixtureAdapter in my Ember app, but when I switch to the RESTAdapter, my URLs no longer work.

The app is a scorekeeping type thing, and I want users to be able to log all the scores without having to be connected to the Web. After the game is finished they can optionally save all the data to the server.

Now, when Ember wants to route to say, matches/:match_id, the ID isn't there because I didn't commit anything to the server/store, so my models don't yet have an ID and I get URLs like: /match/null/games/null

Is this expected behaviour? And if so, is there a workaround? I thought about using model.clientId and then overriding the model hook for each route to try and fetch the Model from the store using the id when present and falling back to clientId. Any other ideas?

UPDATE March 10, 2013:

The following seems to fit my needs and allows to (for now) forget about moving back and forth between local storage and the REST adapter:

App.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.extend({
    namespace: 'api/v1',
    bulkCommit: true,
    generateIdForRecord: function(store, record) {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
      });
    }
  })
});

UUID function taken from: Create GUID / UUID in JavaScript?

解决方案

If a record hasn't been committed, then it shouldn't have an id yet. Furthermore, a url that serializes the application state of viewing that record doesn't make any sense because the record won't exist in another browser until it is committed. You couldn't just paste the url elsewhere and have it load where you left off.

I think what you really want to do is serialize the application state differently (ie. generate a less specific url) when the record is uncommitted. You can achieve this by overriding the serialize method in your route.

For example,

App.PostRoute = Ember.Route.extend({
    serialize: function(model, params) {
        if (model && model.get('isNew')) {
            // corresponds to path '/post/:post_id'
            return { post_id: 'new'}
        }

        return this._super(model, params);
    }
});

Now if you call transitionToRoute('post', post) from your controller, where post is a newly created but uncommitted record, the application state will be serialized to the path /post/new. If you pass it a committed record with an id, it will be serialized as usual.

这篇关于Ember:动态网址中未保存的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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