Does Ember Model attributes support default value? - ember.js

I know that Ember Data attributes can have default values with defaultValue property but does Ember Model support this? If so, how?

It doesn't support default values as of yet. If you really needed to use ember model and still wanted this functionality, you could extend the adapter your using to handle it, or if you are only using the model in a single route you could fix up the model in the afterModel hook (I don't like that idea, because if you ever use that model anywhere else you're hosed). I'd probably modify the ember model code and submit a PR if you can't use ember data. Sorry

Related

How can I model singletons without an id in ember?

I am new to ember and am trying to migrate an existing application, and would like to know what the recommendation is for modeling a single object that will be reused in multiple components on every page. ie: As part of the initial load, I would like to perform a GET request against a URL like 'https://example.com/currentUser' and get an object back like:
{
name: "Users Name"
email: "user#email.com",
profileImg: "http://example.com/pictureOfUser.png"
... snip ...
}
This object will then be used in components for menus, toolbars, and a form for updating it via a post to the same URL.
What is the best way to model this workflow in ember? Given that this is an incidental object and not the focus of most routes, it does not seem to make sense to specify it as the model of them. Also, does ember data handle cases where a model is a singleton and does not have an ID, or would I need to use something like Ember.$.ajax ?
What do you mean by "where a model is a singleton"?
If you use the ember-data default adapter, then yes, a model needs to have an ID, it's part of the JSONAPI spec. If you already have a backend with different conventions, take a look at extending or swapping out the default adapter.
A service is a singleton, and there is nothing preventing you from making an AJAX call there. You would be losing out on all the nice things that come along with ember-data, but, you can do it.

how to pass the Ember dynamic property from beforeModel of route to template handlebar

I want to pass a dynamic variable which is achieved from beforeModel of a route js to corresponding template's handlebar. I know that model data can be passed through model hook. However this is not model data. Also, there is no controller involved since i don't think pass variable from route to controller then template shouldn't be the easiest way.
thanks.
From ember guides beforeModel hook is appropriate for cases,
1) A decision can be made to redirect elsewhere without needing to resolve the model first.
2) Any async operations need to occur first before the model is attempted to be resolved.
so in your case, I will encourage you to do that stuff in setupController hook. if you still want to pass data from beforeModel to template then suggested solution would be, set result in route and get those values and set it in controller through setupController hook. Note: You will get default controller for every route.
This is some route data that you need in your template, that sounds like model data to me!
Also, there is always a controller involved, as one is created by the framework to be the context of the template. What this means is that the model() hook actually sets the model property of the controller, and any {{property}} you look up in your template is looked up in the related controller.
Pass the data along with the rest of the data in the model() hook.

ember - recommended way to fetch a collection of objects to use as menu options without them being considered part of the model

I have a model Foo which has a field barId which has a belongsTo relationship to a model named Bar. I want to have a menu of Bars to set the barId value in my "new" Foo template.
I've already created a select component which displays things properly.
Where is the appropriate place for me to do a JSON fetch of the Bar collection? I'm assuming it should be stored in the controller as that decorates the model and makes extra context available to the template.
Doing an RSVP.hash() in the router's model() doesn't seem appropriate to me, as it would consider the Bar collection part part of the Foo model. Should I be using the router's afterModel()/setupController(), the controller's init() or set this up somewhere else?
Sorry I don't have a jsFiddle or anything, I don't know how to set one up now that my code requires babel.
Any help would be appreciated.
it turned out to be a non-issue as using Ember.RSVP.hash() in my router's model() without using setupController() as it defaults to model.foo, model.bar, etc, which you can access in controllers/templates/etc.
What I was originally worried about was that the other collections/objects I fetched from the store would be stored along with my 'model' when I did a
model.save(), but instead I'd be using model.foo.save(),

How to show all the data changes since the last commit in Ember data?

I know that Ember data's model has the isDirty attribute, but how can I use it to show a list of all the changes/deltas of the data since the last commit?
Tom Dale's talk (listen until 36:44) mentioned that it is possible to tweak the adapter(github, api) or serializer (github, api) hooks to do that. Can anyone give me an example?
current research: I'm using the local storage adapter which adds stuff to the dirty set, which I think might be what I want. It is found in the ember data store, ember data adapter, ember data relationship changes. I am trying to figure out how everything fits together to show the data changes.
If it's any help, Ember Data stores the new (unsaved) values of attributes in the model's _attributes hash. It's quite internal though so you might want to be careful with it - I would n't recommend manipulating it directly.

Overriding Ember's default behavior in {{linkTo}}

The ember guide on {{linkTo}} says:
By default, Ember.js will replace the segment with the value of the object's id property.
I would like to use a different property say profileName(unique String) instead of the id property. Has anyone done this? Any help is appreciated.
Thanks
You have two choices:
You create your custom linkTo handlebars helper, or
Or your hook into the model hook of your route to alter the deserialization, and the serialize hook also in the route for serialization. In this hooks you are able to alter the default behavior from the linkTo helper