0

I am building one simple task application in which I can add new status. Scenario is I have already build the status dashboard using predefined array of objects by calling can.Model findAll method. View is stored in 'dashboard.ejs'. As I create new status, I persist that data into my array of objects and in the save callback new array data (including new one comes up). Now my question is how to refresh my current view with the new returned data

controller=new can.Control({
    init:function(element,options)
    {
       var frag=can.view('dashboard.ejs',this.options)
       this.element.html(frag);
    },
    'someButton click':function(el,ev)
    {
      new ModelCall(formObject).save(function(response)
      {
         //response contains added new object
        //how to refresh the dashboard.ejs with the new response
      });
    }
})

Do I need to create object of new controller again for this purpose or I am missing something?

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56

1 Answers1

0

can.Model .save returns the saved model after completing, so all you have to do is push the new model into your list:

var self = this'
model.save(function (savedModel) {
    self.listOfModels.push(savedModel);
});

In your case I'm not clear where you list of models is, somewhere in this.options? After you push the model into the list the view should refresh automatically.

Sebastian
  • 2,249
  • 17
  • 20
  • Yes. My list of models is in this.options. Now save function returns me the new model object (whole old list+new object). how to update with that response? – Ankur Aggarwal May 12 '14 at 06:57
  • I made use of this http://stackoverflow.com/questions/22350543/canjs-how-to-refresh-a-model?rq=1 on my view :) – Ankur Aggarwal May 12 '14 at 17:23
  • EJS should update this for you, make sure you are using .each and .attr in your ejs template – Sebastian May 12 '14 at 22:47
  • In reference to http://stackoverflow.com/questions/22350543/canjs-how-to-refresh-a-model?rq=1, How can I refresh the data again and again for findAll()? I want to fetch the records again after some operation has been performed on it – Ankur Aggarwal May 13 '14 at 08:51