Titanium JIRA Archive
Alloy (ALOY)

[ALOY-672] Alloy for binding existing Model instances to Model element

GitHub Issuen/a
Resolution Date2014-01-21T15:16:21.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsModels, Runtime
Labelsalloy, model, view
ReporterTiman Rebel


Make 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


  1. 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 instances

    To be able to use multiple concurrent instance I see 2 solutions:

    A) Custom global instance

    We could specify a custom global instance using the existing src attribute:
       <Model id="myBook" src="Alloy.Globals.books.someBook" />
    Which by use of '.' in src would trigger Alloy to compile into:
       $.myBook = Alloy.Globals.books.someBook;

    B) Local instance from singleton collection

    Another way could be to load a model from a existing collection singleton:
       <Model id="myBook" src="book" instance="123" />
    Which by having another value then true (and no . like solution A) would compile to:
       $.myBook = Alloy.Collections.instance("book").book.get("123");
    Both A) en B) could be implemented at the same time.
  2. Tony Lukasavage 2014-01-20

    [~timanrebel] you have the following in your gist:
        * Does not work
       // $.user = args.user;
    Please elaborate. What actually happens when you try the direct assignment?
  3. Timan Rebel 2014-01-21

    It overwrites the model created by Alloy, including its bindings and nothing happens
  4. 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

JSON Source