Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linkTo helper does not notice changing ember-data id #2241

Closed
Goltergaul opened this issue Mar 7, 2013 · 6 comments
Closed

linkTo helper does not notice changing ember-data id #2241

Goltergaul opened this issue Mar 7, 2013 · 6 comments

Comments

@Goltergaul
Copy link

when you have a linkTo helper and pass it an ember-data model that is newly created (has no id) then it links to /route_name/null. so far so good. when i now persist the model to the database the model gets an id. but the link does not notice the new id and stays the same.

this is how i persist the model:

 var transaction = this.get("store").transaction();
    var newPlaylist = transaction.createRecord(MB3.Playlist, {
      name: name
    });
    transaction.commit();

and this is the template:

{{#linkTo playlist this}}{{name}}{{/linkTo}}
@rlivsey
Copy link
Contributor

rlivsey commented Mar 7, 2013

Think this is a dup of #2181

@wagenet
Copy link
Member

wagenet commented Mar 7, 2013

@rlivsey, looks like it.

@wagenet wagenet closed this as completed Mar 7, 2013
@Goltergaul
Copy link
Author

mhh it states there that: "This will of course not handle the case when the actual attribute, like an id, changes, but it will help in most cases. Most of the time you use id and most of the time id does not change."

but i'm exactly talking about the id here

@drogus
Copy link
Contributor

drogus commented Mar 8, 2013

@Goltergaul this is a bit different case. In your situation I would do something like:

{{if playlist.id}}
  {{#linkTo "playlist" playlist}}{{playlist.name}}{{/linkTo}}
{{/if}}

The problem is that you should not even generate a link if you don't have id, because it will just fail without an id.

@drogus
Copy link
Contributor

drogus commented Mar 8, 2013

@Goltergaul actually, I see what you mean now - you may want to still render the link, just with href="#" if an object was not saved yet - it may not be linkable, but it will be clickable.

Unfortunately, at this point it's rather hard to fix this in Emebr. In order to generate a path for linkTo, Ember needs to go through each route and run serialize method. Because we can't easily analyze which params are needed for serialize (most of the time it's id, but maybe it's something else?), we can't refresh href accordingly.

Despite the technical problems behind this, I strongly believe that this should be solved by Ember, not the end user.

My proposal would be to always check for an id in addition to a param itself in #2181. It will handle most of the cases and leave us only with non id attributes (for example TravisCI uses repository.slug in an url) and which I would solve by allowing declarative approach, either in routes, like:

App.RepositoryRoute = Ember.Route.extend({
  serializeProperties: ['slug']
});

or by making it easier to reload link in the template. Helper like with, but without changing context would be helpful.

@Goltergaul
Copy link
Author

@drogus the workaround with an if is a good idea thanks. I agree with you on the rest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants