Keep models in sync


#1

Hi,

Am starting to learn Ember and decided to follow todo app, but just to make it a bit diff decided not to go with Ember data. So far so good, but am bit stuck, my models are kept in sync. Any chance you could let me know the best way of doing it?

The problem it self looks like when I open route with active todos and add one ‘todo’ it is’t reflected in App.TasksActiveRoute but is in App.TasksIndexRoute. Am sure there is a easy way to do so, but couldn’t find it yet.

Also can you give me a hint how to force the models to reload, shall I do it in setupController in the route?

My code looks like:

App.TasksRoute = Ember.Route.extend({
   	model : function() {
		return App.Task.findAll();
	}
});

App.TasksIndexRoute = Ember.Route.extend({
	model : function() {		
		return this.modelFor('tasks');
        }
});

App.TasksActiveRoute = Ember.Route.extend({
	model : function() {
		return App.Task.findActive();
	},
	//use the same template as for index route
	renderTemplate: function(controller){
		this.render('tasks/index', {controller: controller});
	}
});

Here is my model:

App.Task = Ember.Object.extend();
App.Task.reopenClass({
	findAll: function() {
	    return this.find("/api/note/notes/44");
	},
	findActive: function() {
		return this.find("/api/note/notes/44/active");
	},
	findComplete: function() {
		return this.find("/api/note/notes/44/complete");
	},
	find : function(url) {
		console.log("find:", url);
		//var results = Ember.ArrayProxy.create({ content: [] });
		var promise = new Ember.RSVP.Promise(function(resolve, reject){
			$.getJSON(url).then(function(response) {
				var results = [];
				if(response) {
					response.forEach( function (item) {
						results.pushObject( App.Task.create(item) );	    	  
					});
				}
				resolve(results);
			}, function(error, txtMsg, xhr){
				reject(error, txtMsg, xhr);
			});
			
		});
		return promise;
	},
	
	add: function(taskName) {
		console.log("Model:Adding:", taskName);
		
		var request = $.ajax("/api/note/notes", 
				 { data : JSON.stringify({note: taskName, UserId: 44}),
				   contentType : 'application/json',
				   type : 'POST'});
		return request;
	},
	
	update: function(task) {
		var request = $.ajax("/api/note/notes/"+task.Id, 
				 { data : JSON.stringify(task),
				   contentType : 'application/json',
				   type : 'PUT'});
		return request;
	}	
});

the rest of the code is pretty much the same as in the tutorial.