Uploaded image for project: 'Alloy'
  1. Alloy
  2. ALOY-1477

Regression: Missing model attributes cause error on collection data binding

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: alloy 1.8.0, alloy 1.8.3
    • Fix Version/s: Release 5.2.2, alloy 1.8.4
    • Component/s: Models
    • Labels:
      None
    • Sprint:
      2016 Sprint 07 Tooling

      Description

      ALOY-1369 solved one data-binding issue, but uncovered another.

      _.template() demands all variables used in a template to exist. Now ALOY-1369 uses this method apps will error if this is not the case. A very typical use case is when you add a new model to a collection and use an auto incremented primary key. In this case that field will be empty when Alloy first updates the UI in response to create. Once the model has been synced to the DB Alloy would update the UI again to show the now populated field. But since Alloy 1.8.0 it would never get there because of the error.

      The solution is likely to be found in how we use .template() for model data-binding. Here don't pass the individual model fields as variables but the object as a whole. .template() seems to have no problem with properties of that object being empty and simply renders an empty string.

      index.xml

      <Alloy>
      	<Model src="mymodel" />
      	<Collection src="mymodel" />
      	<Window>
      		<ScrollView layout="vertical">
      			<Label top="30" text="{mymodel.id} - {mymodel.title}" />
      			<ListView>
      				<ListSection dataCollection="mymodel">
      					<ListItem title="{id} - {title}" />
      				</ListSection>
      			</ListView>
      			<Button onClick="createModel">Create Model</Button>
      		</ScrollView>
      	</Window>
      </Alloy>
      

      index.js

      Alloy.Models.mymodel.set({
        id: 0,
        title: 'mod TITLE'
      });
       
      Alloy.Collections.mymodel.reset([{
        id: 0,
        title: 'col TITLE'
      }]);
       
      $.index.open();
       
      function createModel() {
        Alloy.Collections.mymodel.create({
          title: 'crt TITLE'
        });
      }
      

      mymodel.js

      exports.definition = {
        config: {
          "columns": {
            "id": "INTEGER PRIMARY KEY AUTOINCREMENT",
            "title": "TEXT"
          },
          "adapter": {
            "type": "sql",
            "collection_name": "mymodel",
            "idAttribute": "id"
          }
        }
      };
      

      Template code

      [INFO]  var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
      [INFO]  with(obj||{}){
      [INFO]  __p+='{mymodel.id} - {mymodel.title}';
      [INFO]  }
      [INFO]  return __p;
      [INFO]
      [INFO]  var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
      [INFO]  with(obj||{}){
      [INFO]  __p+=''+
      [INFO]  ((__t=(id))==null?'':__t)+
      [INFO]  ' - '+
      [INFO]  ((__t=(title))==null?'':__t)+
      [INFO]  '';
      [INFO]  }
      [INFO]  return __p;
      

      Error

      [ERROR] Script Error {
      [ERROR]     column = 10;
      [ERROR]     line = 4;
      [ERROR]     message = "Can't find variable: id";
      

      Found by SO user:
      http://stackoverflow.com/questions/36274177/alloy-data-binding-error-after-alloy-1-8-upgrade

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                fokkezb Fokke Zandbergen
                Reporter:
                fokkezb Fokke Zandbergen
                Reviewer:
                Feon Sua Xin Miao
              • Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Backbone Issue Sync is enabled for your project, but we do not have any synchronization info for this issue.

                    Git Integration