Skip to content

Instantly share code, notes, and snippets.

@edtoken
Last active August 29, 2015 14:03
Show Gist options
  • Save edtoken/995a95dc5495f64fe5ca to your computer and use it in GitHub Desktop.
Save edtoken/995a95dc5495f64fe5ca to your computer and use it in GitHub Desktop.
var apiRelationalCollection = {
method:false,
type:false,
loading:function(data){
return data;
},
data:false,
relational:false,
};
var apiRelationalModel = {
method:false,
type:false,
loading:function(data){
return data;
},
data:false,
relational:false,
};
var createItem = function(item, callback)
{
var url = item.type + '/' + item.group + '/' + item.name;
var Class = this.app[item.type][item.name];
require([url], function(Func){
Class = new Func();
return callback(Class);
});
}
apiRelationalInitModel = function(){
var that = this;
};
apiRelationalInitCollection = function(data, success, error, relationals){
var that = this;
if(!this.apiRelational){
return success(this);
}
var setData = function(resp)
{
this.reset(this.apiRelational.loading.apply(resp));
}
var itemAjaxMethod = function(callback){
var that = this;
if(!this.apiRelational || !this.apiRelational.method){
callback();
}
var method = that.app.api[that.apiRelational.method];
if(!method){
callback();
}
data = _.extend({}, this.apiRelational.data);
if(method){
method(data, function(resp){
setData.call(that, resp);
callback();
});
}else{
callback();
}
};
var createItem = function(item, callback)
{
var that = this;
var url = item.type + '/' + item.group + '/' + item.name;
var Class = this.app[item.type][item.name];
if(Class){
return callback(Class);
}
require([url], function(Func){
Class = new Func();
that.app[item.type][item.name] = Class;
Class.apiRelationalInit({}, function(resp){
return callback(Class);
}, function(resp){
// console.log('createCollection false', resp);
});
});
}
var createObjItems = function(items, out, callback){
if(items.length > 0){
var item = items.pop();
var Class = this.app[item.type][item.name];
if(Class !== undefined){
return createObjItems.call(that, items, out, callback);
}
createItem.call(that, item, function(itemClass){
out.push(itemClass);
return createObjItems.call(that, items, out, callback);
});
}else{
callback(out);
}
};
if(this.apiRelational.relational){
var items = this.apiRelational.relational;
if(relationals){
items = items.concat(relationals);
}
createObjItems.call(that, items, [], function(createditems){
itemAjaxMethod.call(that, function(){
success();
});
});
}else{
success();
}
};
Backbone.Model.prototype.apiRelational = apiRelationalModel;
Backbone.Collection.prototype.apiRelational = apiRelationalCollection;
Backbone.Model.prototype.apiRelationalInit = apiRelationalInitModel;
Backbone.Collection.prototype.apiRelationalInit = apiRelationalInitCollection;
/**
* demo collection
*/
define([
'jquery',
'underscore',
'backbone',
'models/garage/list_item'
], function($, _, Backbone, ItemModel){
var CarsListCollection = Backbone.Collection.extend({
model:ItemModel,
name:'Cars',
apiRelational:{
method:'getCars',
loading:function(){
return this.cars;
},
data:{},
relational:[],
},
initialize:function(){
this.app.collections.Cars = this;
}
});
return CarsListCollection;
});
/**
* in view
*/
this.collection = listCollection;
this.collection.bind('reset', this.renderCollection, this);
this.collection.apiRelationalInit({}, function(resp){
// trigger collection.reset here so - this position - render collection
}, function(resp){
// console.log('createCollection false', resp);
});
@edtoken
Copy link
Author

edtoken commented Jul 7, 2014

relational:[
{
type:'collections',
group:'schedule',
name:'Schedule'
},
{
type:'collections',
group:'garage',
name:'Cars'
}
],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment