Search code examples
backbone.js

Making two views update with a single ajax request


I have a backbone application in which two views at the present are polling on the same API. The API in case of a state change in the DB returns data otherwise it doesn't. If the request from one view reads the state change then the other one wouldn't get it as to the API that state change information has already been read. This creates a sort of race between the two views. So, I have been thinking about moving the API call to a single location and then on a response with a state change, call functions in two views which do further processing on that information. So, what is the best way to do this? Should I make a separate view and make it inherit the two views and then call their functions?


Solution

  • have same model for two different views , then listen on change Event from that model, have this event listener in your both Views

    this.model('change',this.takeAction);