[ALOY-672] Alloy for binding existing Model instances to Model element
|Labels||alloy, model, view|
DescriptionMake it possible to bind an existing Model instance to a Model element in Alloy. Currently it is possible to copy the attributes into the Model element, but we do not get data binding on the original Model instance. See: https://gist.github.com/timanrebel/5643468
- Fokke Zandbergen 2013-05-27
In order to use an existing model instance, we need to be able to refer to some existing object in memory or load one from a persistant storage.
Single instance (singleton)One solution is to use
Alloy.Models.instance, which works fine and is explained on: http://fokkezb.nl/2013/05/27/bind-existing-model/ But this only works for a single concurrent instance per model, since it will take it's name from the name of the model.
Multiple concurrent instancesTo be able to use multiple concurrent instance I see 2 solutions:
A) Custom global instanceWe could specify a custom global instance using the existing
Which by use of '.' in
<Model id="myBook" src="Alloy.Globals.books.someBook" />
srcwould trigger Alloy to compile into:
$.myBook = Alloy.Globals.books.someBook;
B) Local instance from singleton collectionAnother way could be to load a model from a existing collection singleton:
Which by having another value then
<Model id="myBook" src="book" instance="123" />
.like solution A) would compile to:
Both A) en B) could be implemented at the same time.
$.myBook = Alloy.Collections.instance("book").book.get("123");
- Tony Lukasavage 2014-01-20
[~timanrebel] you have the following in your gist:
Please elaborate. What actually happens when you try the direct assignment?
/** * Does not work */ // $.user = args.user;
- Timan Rebel 2014-01-21 It overwrites the model created by Alloy, including its bindings and nothing happens
- Tony Lukasavage 2014-01-21 This will eventually be resolved a bit more robustly with a programmatic binding API (ALOY-931). In the meantime though, you can do this with straight Backbone calls. I forked your gist and modified it to do so (untested). Even if it needs tweaking, though, it should show you how you can achieve your goal of dynamically changing the binding: https://gist.github.com/tonylukasavage/8541502