user1418079
user1418079

Reputation: 155

backbone multiple views with the same model

I am doing an only front end project with backbone. And at one point I want to do the following :

The problem is that I don't have any backend so I never save the inputs of the user, How can I change the view and still have the data of the form?

I was thinking about storing it temporary into localstorage but it's not really a good solution for perfs...

Thanks

Upvotes: 0

Views: 895

Answers (2)

Kevin
Kevin

Reputation: 1

Im also doing an one view web app with backbone.

I think the point of your problem is you really has only one page but load different views into this page. Not change to another page.

I suppose your app url is http://xxx.xxx.xxx/#first_view. and use backbone Router to change views

  • If you just want keep the data until user refresh browser. Just save them into a Global js variable. Once you use something like

window.location = Global.getBaseURL() + "#second_view" to change your view. And you actually load the "second_view" by ajax and put html into current page. You never lose your js variable.

  • If you want keep data even user refresh or go to another page. You have to use sessionStorage. Save data into JSON format and convert them back to js variable once you finish loading the new page.

Upvotes: 0

Peter Lyons
Peter Lyons

Reputation: 146164

  1. Router creates a model instance
  2. Router passes that model instance to the form view constructor options
  3. Router binds event listener view.on('formComplete', this.storeModel)
  4. Router renders & attaches form view
  5. User fills out form view
  6. Form view sets the data from the form into the model
  7. Form view triggers route or event (like this.emit('formComplete', this.model);)
  8. Router's storeModel handler function takes the same model instance, stores it as this.model temporarily on the router, and then navigates to the graph view.
  9. In the graph view route handler method, router passes this.model it to the graph view contsructor options, render, attach

This is sort of using your router as an in-memory data cache, but since you have no back end, you need to store data somewhere.

Upvotes: 1

Related Questions