I need to use a datacontext in serveral controllers.
The datacontext can be change in different ways (by ajax call, by user/view, by server).
All controllers using the datacontext should be notified when the data in the datacontext has changed.
datacontext:
var userDataContext = { firstName: "John", lastName: "Doe" };
I simulated in every example a backend change with setTimeout
setTimeout(function() {
console.log("change firstname");
userContext.firstName= "Bart";
}, 3000);
Here is an fiddle example in Knockout, where it works fine.
http://jsfiddle.net/8ct0L1zu/4/
I also tried to do it with Angular but it partially work.
When I pass the complete object to the controller(by reference), changes works between controllers.
When changing the datacontext in javascript/backend, it doesn't change my views.I also prefer to have only the firstname and lastname in the controlers scope, and not the complete object.
But in this case, nothing works.
Reason: In our application there will be a lot of datacontexts with arrays of big objects.
http://jsfiddle.net/19xv3skn/1/
To really make it work, I used Angular with knockout. Angular for databinding and knockout for datacontext. This works fine, but I don't really like this solution.
http://jsfiddle.net/7chp5xLa/2/
Is there a best practice or better way to work with a observable datacontext in Angular?