S
S
Sergey Pronin2016-08-25 08:03:20
backbone.js
Sergey Pronin, 2016-08-25 08:03:20

What can replace the code construct?

There is an old proven code for synchronizing the backbone model with the database

var config = new App.Models.Config(option, data);
config.fetch().then(function () {
  new App.Views.Config({model: config});
}, App.ajaxError);

Briefly: the model first updates the data from the server, and then only renders the view.
After connecting the backbone-localstorage library, it replaces sync with its own function, which, as I understand it, does not return anything, so I cannot use chain function calls through a dot.
What solutions can you suggest?
In general, for the solution, I decided to use this version of the code:
config.once('sync', function(config) {
            new App.Views.Config({model: config})
        });

        config.fetch();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-08-25
@stDragon

Since reading from localStorage is synchronous, there is no need to use promises or callbacks:

var config = new App.Models.Config(option, data);
config.fetch();
new App.Views.Config({model: config});

In order to use ajax and localStorage at the same time, you need to wrap the call in a Promise:
var config = new App.Models.Config(option, data);
Promise.resolve(config.fetch()).then(function () {
  new App.Views.Config({model: config});
}, App.ajaxError);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question