Ember Data - destroy model from controller - ember.js

I have a Lead model defined with DS.Model and I noticed something weird when I was working with creating my app. When the REST resource doesn't return a resource in subsequent calls, the old data stays around. I tried to solve this by doing:
var model = this.get('model');
model.forEach(function(model) {
model.destroy();
});
However, nothing happens. I have searched around but can't find the proper way to clear out a specific model cache and then refresh it with find(). I am using ember data 0.13

Please try this:
model.deleteRecord();

Related

can i createRecord() without using save()?

I'm learning ember from the scratch and i stumbled upon something i can't quite understand:
const newNote = this.store.createRecord('note', {
content: 'once upon a time...',
});
newNote.save().then(this._redirectToNewNote.bind(this));
Is the result of createRecord in the database before saving, or is it like a dummy object? can I use createRecord without saving?
Yes. you can use it without saving, do remember it will be removed from store when you refresh page.
From ember guide
createRecord is used for creating new records on the client side. This will return a new record in the created.uncommitted state. In order to persist this record to the backend you will need to call record.save().

Using store.all adds items to controller

I'm using store.all to find all the current instances of an model in my store and then setting it as the model for the route in my controller like this:
model: function() {
this.store.all('activeShip');
}
That works fine, but I'm having some odd behavoir when I create a new model instance for that type. When I do, it's automatically added to the controllers model even though I never addObject. I've created a little demo here to see what I mean. As soon as you create a record, it's included in the model and updated in the view. I figured store.all would just run once, gather all the records and be done; not continue to update as new records are created.
Is this the desired behavior, a bug or am I doing something wrong? Thanks.
EDIT: Think these two issues my be related: #2375, #2542.
This is expected behavior, but maybe not desired in your situation. It's actually one common criticism of Ember Data--the inability to "fork" a model or RecordArray until the changes are persisted.
Secondly, a call to this.store.all returns a DS.RecordArray, which actually updates when new records are added to the store. If you don't want to show new records, consider using something like return this.store.all('post').filterProperty('isNew', false); to keep it from showing new records. Once they're persisted on the server, it will show in the array, but not before.
P.S. You're not actually "setting it as the model" unless you return this.store.all('activeShip'); (note return).

Reloading a route's model with server json data and need ember-related opinion

I’m building a map with a search function. Basically, I’d like to store objects from the server within my ember app so that whenever I search for something that collection updates itself with the results from the server so the related view updates itself. It’s all on one page.
So far I have an Application Controller, and a Results ArrayController. Data is shown from the Results Controller. Now I’d need that when a search is requested, it gets JSON from the server and updates the results collection.
First question would be:
How would you build that?
I did a v1 with jQuery only and started a new one with Ember but I’m lost as of how structure-wise should I build it.
I built a small jsbin based on what I have here: http://emberjs.jsbin.com/IYuSIXE/1/
Second question:
How would I change a route's model content? Am I going in the wrong direction?
Thanks a lot
You can do both 1 and 2 with query params, check the documentation here https://github.com/alexspeller/website/blob/a96d9afe4506454b155cc64299e86e558ce3c9f1/source/guides/routing/query-params.md
When your route calls the model it will pass the query params, you can do your search against them
model:function( params, queryParams, transition ) { callToYourBackedEndWithQueryParams}
Second question: How would I change a route's model content? Am I
going in the wrong direction?
When the search is requested, in an action you can call this.transitionTo({queryParams: {sort: 'asc'}});, that will fire up again the model hook and you can do the query against your server again.
What I was looking for is a way to change the model on-the-fly.
So basically if I have this:
App.ResultsRoute = Ember.Route.extend({
model: function() {
// empty array that will contain results
return [];
}
});
I can do this to set the content of the model:
this.get('model').setObjects([{}, {}, {}]);
That way I can dynamically play with the models, load them with objects coming from almost anywhere.

Dealing with complex model

I am working on a new Ember.js project using Rails as backend, and Mongodb as database. Basically it's Starcraft 2 replay analyzer, like ggtracker.com (which powered by angularjs)
Current data structure for my model:
http://paste.kde.org/pd3582db1/
I don't know even how to begin defining it, seems like ember-data is missing a complex type field, and defining each sub model will take like forever (The current model doesn't contain the entire data)
Thanks in advance,
BBLN.
You don't have to use Ember-Data, you can use pojos. Ember-Data is just one of the many "simplified" ways of using models, there is also Ember-Model. That being said, if you created a new Ember object with that huge pojo you'd be accessing it something like this:
var someModel = Ember.Object.create(bigOlJSON);
someModel.get('players.firstObject.abilities.firstObject.blahblahblahblah');
or you could slowly build it up
var players = [];
someModel.get('players').forEach(function(player){
players.push(Ember.Object.create(player));
});
//At this point players is loaded with a slew of player data
That seems a little too monstrous to me.
I'd think it would almost be beneficial to map out all of the models, and set them as embedded models.
See this post for embedded records for Ember Data: Ember-data embedded records current state?

EmberJS Model find not up-to-date with underlying store

For a simple overview screen I have a developed a route that sets up a controller that does an App.Location.find().
App.LocationsIndexRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set('content', App.Location.find());
},
renderTemplate: function() {
this.render('locations.index',{into:'application'});
}
});
I naively assumed that this would simply go to the store and fetch me all the records, giving me an up-to-date view of the records.
Apparently not....
When an external process starts removing records from the database,
the App.Location.find() keeps on returning these deleted records.
(although the REST call doesn't show them anymore)
If an external
process starts adding records to the database, the
App.Location.find() picks them up.
If I delete the records form
within the Ember app itself the model is correctly updated.
How should I deal with this in my Ember app ? I'd like to have an up-to-date view on whatever is in my database. Right now I need to refresh the page (F5) to get an up to date view. Using the linkTo helpers shows me the stale data.
This seems to be yet another trivial thing that I completely missed in EmberJS. Is it somewhere mentioned in the docs why it behaves like that ? I guess there is a valid philosophy behind this behavior.
My overview screens is simply interested in showing the most up-to-date data. If a record is no longer in the DB the model should not return it anymore.
I've added a sample project in Github that is having this issues.
Try unloading all of the data from the store before you call find():
this.store.unloadAll('widget');
this.store.find('widget');
That will fully refresh your store to reflect what's on your server.
Have you tried App.Location.query() instead of App.Location.find()?