• Register
  • Log In
  • Home
  • Quick Start
    • Configurator
    • Download YUI 3
  • Documentation
    • User Guides
    • Examples
    • API Docs
    • Environments
    • Tutorials
  • Community
    • Gallery
    • Blog
    • Forums
    • YUI Theater
    • Calendar
  • Contribute
    • YUI on GitHub »
    • File a Ticket
    • View Tickets
    • Dashboard
  • Other Projects
    • Shifter »
    • Yogi »
    • YUI 2
    • YUI Doc »
    • YUI Test
    • YUI Website
    • YUI Compressor »
    • YUI Builder »
    • YUI PHP Loader
    • Grid Builder »
    • Skin Builder »
  • YUI
  • >
  • Community
  • >
  • Gallery

Gallery

Modules

  • Home
  • Featured
  • Popular
  • New
  • All

Documentation

  • Yogi Documentation
  • Shifter Documentation
  • Developer Guide
  • Module Setup

Tag Cloud

Context Navigation

    Build Tag: gallery-2012.04.18-20-14

    Return to Model Relate

    YUI({
        //Last Gallery Build of this module
        gallery: 'gallery-2012.04.18-20-14'
    }).use('gallery-model-relate', function(Y) {
    	var CompanyModel, EmployeeModel;
     
    	// example company model that has a toMany relationship 'employees' to EmployeeModel
    	CompanyModel = Y.Base.create('companyModel', Y.Model, [Y.ModelRelate], {}, {
    		ATTRS: {
    			name: {}
    		},
    		RELATIONSHIPS: {
    			employees: {
    				type: 'toMany',
    				key: 'id',
    				relatedModel: 'EmployeeModel',
    				relatedKey: 'companyId'
    			}
    		}
    	});
     
    	// example employee model that has a toOne relationship 'company' to CompanyModel
    	EmployeeModel = Y.Base.create('employeeModel', Y.Model, [Y.ModelRelate], {}, {
    		ATTRS: {
    			name: {},
    			companyId: {}
    		},
    		RELATIONSHIPS: {
    			company: {
    				type: 'toOne',
    				key: 'companyId',
    				relatedModel: 'CompanyModel',
    				relatedKey: 'id'
    			}
    		}
    	});
     
    	// creating an instance of a model extended with ModelRelate
    	// automatically registers it with the ModelStore
    	var foo = new CompanyModel({id: 1, name: 'Foo Inc.'});
    	var joe = new EmployeeModel({id: 1, name: 'Joe Foo', companyId: 1});
     
    	// use the getRelated method to return models matching the
    	// 'employees' relationship defined on CompanyModel.  In
    	// this case, a ModelList instance since 'employees' is defined
    	// as a 'toMany' relationship
    	var fooEmployees = foo.getRelated('employees');
     
    	// fooEmployees is a modelList containing the joe model
     
    	var larry = new EmployeeModel({id: 2, name: 'Larry Bar', companyId: 1});
     
    	// fooEmployees now contains 2 employee models, joe and larry
     
    	var joeCompany = joe.getRelated('company');
     
    	// joeCompany is the Foo Inc. company model
     
    	// aww, larry switched jobs
    	larry.set('companyId', 2);
     
    	// since we updated the companyId on the larry model, it no longer matches the companyId
            // on the fooEmployees relationship.  larry has been removed from the fooEmployees
            // relationship which now contains only the joe model.  larry still exists, just not as
            // part of the fooEmployees relationship
     
     
    	// note:  Relationships are not actually created until the first call to 'getRelated'
    	//        so defining a relationship and adding a bunch of related models prior to
    	//        the first call to 'getRelated' does not incur any overhead
     
     
    	// Now, lets say larry starts his own business
    	var larryCo = new CompanyModel({name: 'LarryCo Ltd.'});
     
    	larry.set('companyId', larryCo.get('id'));
     
    	// model-relate changes the definition of a 'new' model.  Instead of setting id=null
    	// new models have id=clientId so we always have a valid id for the relationship
     
    	// now when we save larryCo to our database
    	larryCo.save();
     
    	// the relationship knows to update all the related models to the new id
    	larryCo.get('id'); // == 5768 as assigned by the database
    	larry.get('companyId'); // == 5678
    	larryCo.getRelated('employees'); // modelList containing the larry model
    	larry.getRelated('company'); // the larryCo model
     
    	// if a related key changes not as the result of a new model being created, the relationship
    	// is broken and will be refreshed with models in the store matching the new key
    });

    Build Output

    /home/y/libexec/ant/bin/ant all
    Buildfile: build.xml
         [echo] Starting Build For gallery-model-relate
    
    clean:
    
    init:
        [mkdir] Created dir: /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp
         [echo] Starting Build For gallery-model-relate
    
    -lint-server:
         [echo] Starting Build For gallery-model-relate
    
    -node:
         [echo] For faster builds, install Node.js.
    
    -concatdebug:
         [copy] Copying 4 files to /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp/ant
       [delete] Deleting directory /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp/ant
    
    -registerdebug:
         [copy] Copying 1 file to /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp
    
    -prependdebug:
    
    -appenddebug:
    
    builddebug:
    
    -createcore:
         [copy] Copying 1 file to /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp
    
    -loggerregex:
    
    buildcore:
    
    -rollupjs:
    
    -concatskins:
    
    -buildskins:
    
    -rollupcss:
    
    buildskins:
    
    -buildlangs:
    
    -rolluplangs:
    
    buildlangs:
    
    build:
    
    minify:
    [yuicompressor] Compressing /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp/gallery-model-relate.js
    [yuicompressor] 
    [yuicompressor] [WARNING] Try to use a single 'var' statement per scope.
    [yuicompressor] .preventDefault();}}}; ---> var  <--- ToOneRelationship={type:"toOne",_initRelated:function
    [yuicompressor] 
    [yuicompressor] [WARNING] Try to use a single 'var' statement per scope.
    [yuicompressor] (e);}}}}; ---> var  <--- store=Y.ModelStore;functionModelRelationshp(config
    [yuicompressor] 
    [yuicompressor] [WARNING] Try to use a single 'var' statement per scope.
    [yuicompressor] augment(ModelRelationshp,Y.EventTarget); ---> var  <--- RELS="relationships",REL_PREFIX="_rel_";functionModelRelate
    [yuicompressor] Compressed to /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp/gallery-model-relate-min.js
    
    lint:
         [echo] Using Rhino. Install nodejs to improve jslint speed, or skip with -Dlint.skip=true
         [java] Running JSLint on : /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/src/gallery-model-relate/build_tmp/gallery-model-relate.js
         [java]  
         [java] 
         [java] 	239, 62: Expected an assignment or function call and instead saw an expression.
         [java] 	this.related && this.related.set(name, value, options);
         [java] 
         [java] 	1096, 59: Expected an assignment or function call and instead saw an expression.
         [java] 	callback && callback.apply(null, arguments);
         [java] 
         [java]  
         [java] 
    
    local:
    
    deploybuild:
         [copy] Copying 3 files to /home/y/var/builds/workspace/gallery/build_tmp/src/yui3-gallery/build/gallery-model-relate
    
    deployassets:
    
    deployskins:
    
    deploylang:
    
    deploydocs:
    
    deploy:
    
    all:
    
    BUILD SUCCESSFUL
    Total time: 3 seconds

    © 2006-2013 Yahoo! Inc. All rights reserved.
    All code on this site is licensed under the BSD License unless stated otherwise.
    About This Site · Security Contact Info