• 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

    YUI Library is not responsible for bugs or support with this module. It is available as a free service. For support please contact the module owner with the provided links.

    Model Relate (gallery-model-relate) on cdn

    Last Updated: 04/11/12
    + 6 -

    Lee

    YUI Contributor

    See 3 more by this user.

    Featured Item

    Created: 11/8/11
    Last CDN Push: 04/18/12
    Build Tag: gallery-2012.04.18-20-14
    Project: YUI 3
    License: YUI BSD
    YUI Version: 3.5.0
    Free for use.

    Y.Model extension that allows you to define relationships between different types of models. Uses gallery-model-store to automatically update relationships as models are created and deleted.

    this module is not backward compatible with 3.4.x due to an attribute api change in 3.5. If you need 3.4.x compatibility, use gallery version gallery-2012.04.10-14-57 or https://github.com/yui/yui3-gallery/commit/7b443d88688e91b4e735f90919eef2a16a412e35

    • Tags:
    • bwg
    • relate
    • related
    • relationship
    • model-store
    • model
    • store
    • Download
    • Docs
    • Homepage
    • Bugs
    • Source
    • Example
    • Forum
    • History

    Example of a toMany and toOne relationship, as well as showing how a 'new' (unsaved) model can still have relationships and maintain those relationships after its key(id) has changed.

    Code Sample

    <script src="http://yui.yahooapis.com/3.5.0/build/yui/yui-min.js"></script>
    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
    });

    © 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