ember data isLoad on collections - ember.js

I use emberjs-data
allProcessPointsAreLoaded:(->
#get("reportDefinition.virtualColumns").everyProperty("processPoint.isLoaded", true)
).property("reportDefinition.virtualColumns.#each.processPoint.isLoaded")
my property is not updated when processPoint.isLoaded changed to true. Any thoughts?

The latest ember-data has an isLoaded property on the whole content collection, instead of just each field. In my view I monitor the path 'controller.content.isLoaded' to determine when all the content is there.

I believe that #each can only currently support one nested property "reportDefinition.virtualColumns.#each.processPoint" but not "reportDefinition.virtualColumns.#each.processPoint.isLoaded". Here's the github issue that I found.
I've worked around it by adding a property to proxy to isLoaded. So you would have something like:
processPointLoaded: function() {return this.get('processPoint.isLoaded')}.property('processPoint.isLoaded')
I hope that helps!

Related

Not possible to use shorthand route handlers if RestSerializer is used? (ember-cli-mirage)

I set up a simple Ember Twiddle to show you my error that is occurring when trying to update a model.
It's considerable that I'm using ember-cli-mirage for mocking the data.
According to the docs, I created a shorthand route that should handle the PUT request.
It does, but with the error: Your handler for the url /api/shops/1 threw an error: Cannot convert undefined or null to object
When using the JSONAPISerializer, everything is working with shorthands (mirage/config.js) and I'm able to update models, but in my case I have to use the RESTSerializer with serialized IDs in the responses.
The request payload when I'm sending the model's attrs are without Id at the end of the property name, f.e.:
// attrs object in PUT request
{
name: "Shop 1",
city: "1" // belongsTo relationship,
}
Now Mirage is trying to find those properties on the respective database model that has to be updated, but cannot find it, because in the database it's cityId and not just city...
I also found this issue report and it’s working, but I was hoping I could avoid something like this. As far as I can remember, in previous versions of ember-cli-mirage (v0.1.x) it was also not needed to override the normalize method in the serializer to be able to make use of the RestSerializer with serializedIds…
My question is:
Is there a way to stick to shorthand route handlers only, or do I really have to write a helper or other custom solution only because I have to use the RestSerializer?
That would be really sad, but at least I would know then.
Thanks for your support!
Short answer: it looks like you need the custom serializer for now until the bug fix for it is merged.
Long answer: that issue looks to be an issue that occurred in the 0.2 -> 0.3 upgrade for Mirage, likely because of underlying DB changes made in Mirage. It'll probably get fixed, but for now you'll need to work around it.

Defining buildURL depending on what properties have changed

I would like to redefine my buildURL depending on what properties changed on the same model. For example, if the status changed, I would like to PUT to a certain route, and if the subuser changed, I would like to PUT to another route.
Example :
this.store.find('conversation', conv.id).then(function(conversation){
conversation.set('status', 'opened');
conversation.save();
});
This would use a certain PUT route and this :
this.store.find('conversation', this.get('selectedConv').id).then(function(conversation){
conversation.set('subuser', subuser);
conversation.set('url', subuser.get('email'));
conversation.save();
});
And this would use another PUT route even tho the changes are made on the same model. This is all happening in a controller.
You need to customize your conversation adapter, specifically the urlForUpdateRecord method.
The original method looks like this:
urlForUpdateRecord: function(id, modelName, snapshot) {
return this._buildURL(modelName, id);
},
In this method, you need to examine the snapshot and adjust the URL accordingly.
The latest version of Ember Data has introduced the changedAttributes property. This seems to be what you need.
Good luck!

Computed properties with ember-model?

I have a simple model that worked with ember-data but not sure how to get this to work with ember-model?
Acme.App = Ember.Model.extend(
post_screenshot_url: Ember.attr()
post_screenshot_url_thumb: (->
#get('post_screenshot_url') + '/?thumb=true'
).property('')
)
This used to work with ember-data but now this doesn't work with ember-model. Thoughts?
It's working for me with latest Ember and Ember Model, check this JSBin:
http://emberjs.jsbin.com/obeHimU/3/edit?html,js,output
PS: You probably should add "post_screenshot_url" to the properties call on "post_screenshot_url_thumb" so it gets correctly updated.

Ember.js "isDirty" not being cleared on save with both Epf and Ember-Data (1.0.0.beta.2)

I'm having a problem using Ember. When I change a model, its "isDirty" flag becomes true, which is what I expect.
However, after that its "isDirty" flag is true, even after I save that model.
Here's a minimal Rails + Ember project (so I can actually save the model) that shows the situation:
https://github.com/csterritt/etst
Am I doing something wrong? Is this expected behavior?
Thanks!
Edit: Turns out that, as Jeremy Green pointed out below, the "isDirty" flag works for Ember Data.
And, it works with the current Ember 1.0.0 (standard, not -latest) and Ember Data beta.
I was doing:
isClean: ( ->
! #get("isDirty")
).property("name", "age", "favorite_food")
Which was due to a misunderstanding on my part. Changing this to:
isClean: ( ->
! #get("isDirty")
).property("isDirty")
Works properly.
Unfortunately, this doesn't solve the Epf version's problem. Epf-ites?
Can you post a JSBin demonstrating the issue? Here's a simple JSBin with the FixtureAdapter that shows the isDirty flag being cleared correctly.
http://jsbin.com/ucanam/1058/edit
I also just double checked in one of my apps that's using the RESTAdapter against a real API, and it is also clearing the flag.
[EDIT] : The JSBin that I posted is running ember-data-latest, and my real app is using beta 2.
With regards to EPF, isDirty is currently a volatile computed property. I will change this soon.

How to set a controller property and update a template in Ember.js

I am trying to show a TopController property in a TopView template. In TopView, I have sectionBinding: 'controller.section'.
From my understanding of Ember.js, in TopView, the controller property should refer to my TopController. Yet it seems to refer to ApplicationController? Read on:
In my router, I have router.set('topController.section', 'index');... But that doesn't seem to do anything in this case. Changing it to router.set('applicationController.section', 'index'); works and the {{section}} part in the TopView template changes to "index".
I have created two fiddles showing my issue. The first one doesn't work:
FAULTY -> http://jsfiddle.net/8tQ4q/4/
The second one does work:
WORKS -> http://jsfiddle.net/8tQ4q/5/
The only difference is the topController / applicationController part in router.set(). Any idea what I am doing wrong?
I'm not sure why you're expecting topController to be connected to TopView. You haven't done anything to make this connection. I think you may be confused because connectOutlet('top') would create a TopView that is connected to topController. However, you aren't doing this anywhere in your app.
You also don't need the sectionBinding. If you have a controller defined on your view, it will be the default context.
I think what you want to do is this:
router.get('topController').set('section', 'index');