2 Replies Latest reply on Dec 3, 2013 5:29 AM by Tom Mann

    How does the belongs_to association work in javascript models?

      I was under the impression that the belongs_to association would be used when syncing related models.

       

      If I have a parent model that is defined as

       

      var parentModel = Rho.ORM.addModel(function(model) {

          model.modelName("Parent");

          model.enable("sync");

          model.property("propertyOne", "string");

      });

       

      and a child model that is defined as

       

      var childModel = Rho.ORM.addModel(function(model) {

          model.modelName("Child");

          model.enable("sync");

          model.property("parentId", "integer");

          model.property("propertyOne", "string");

          model.belongs_to("Parent");

      });

       

      Now a parent is created on the local device and has a temporary id. Then a child is created on the local device and the parent is looked up and the parent id (object) is assigned to the parentId field in the child. In this scenario the parent and child have not been synced to the backend yet so the parentId field on the child has the temporary id of the parent.

       

      var parentModel = Rho.ORM.getModel("Parent");

      var parent = parentModel.find(...) #find the parent

       

      var childModel = Rho.ORM.getModel("Child");

      var child = childModel.create({

          parentId: parent.get("object"),

          propertyOne: "test"

      });

       

      Now when the parent is synced with the backend and the temporary id is replaced with the backend id, this is where I would have expected the belongs_to relationship to be updated as well. In this example I would have expected the parentId field on the child model to also be replaced with the parent's backend id.

       

      I must not be understanding how the belongs_to works because as far as I can tell the belongs_to does not seem to trigger any type of update on the child models. If the belongs_to is not how you handle this situation what is the proper way of doing this? If the belongs_to is the proper way of doing this how can I debug this to see why the updates are not being triggered?