Skip to content

Instantly share code, notes, and snippets.

@nazrulworld
Created October 25, 2014 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nazrulworld/40d968925f5d3b490466 to your computer and use it in GitHub Desktop.
Save nazrulworld/40d968925f5d3b490466 to your computer and use it in GitHub Desktop.
var page_app = page_app || {};
(function(){
'use strict';
// Food Group Model
page_app.FoodGroupModel = Backbone.Model.extend({
defaults: {
id: null,
name: null,
slug: null
}
});
// Food Item Model
page_app.FoodItemModel = Backbone.Model.extend({
defaults: {
id: null,
short_description: null,
long_description: null,
food_group_id: null,
is_survey: false,
common_name: null,
scientific_name: null,
protein_factor: null,
refuse_description: null,
fat_factor: null,
foot_note: null,
nitrogen_factor: null,
refuse_ratio: null,
manufacturer: null,
carbohydrate_factor: null
}
});
// Collection
var BaseCollection = Backbone.Collection.extend({
initialize : function(){
Backbone.Collection.prototype.initialize.apply(this);
this.filtersEnabled = {};
this.results = {};
},
values : function(){
var l = [];
var key = this.keyField;
_.each(this.models, function(model){
l.push(model.get(key));
});
return l;
},
key_values : function(){
var l = [];
var key = this.keyField;
_.each(this.models, function(model){
l.push([model.get('id'), model.get(key)])
});
return l;
},
setFilter : function(key, value){
this.filtersEnabled[key] = value;
},
fetch : function(options) {
options['data'] = this.filtersEnabled;
return Backbone.Collection.prototype.fetch.call(this, options);
//this.fetch({data: this.filtersEnabled}, success: success);
}
});
// Food Group Collection
page_app.FoodGroupCollection = BaseCollection.extend({
model: page_app.FoodGroupModel,
url: '/manage/food-groups/'
});
// Food Item Collection
page_app.FoodItemCollection = BaseCollection.extend({
model: page_app.FoodItemModel
});
})();
(function($){
'use strict';
// Item Slection Modal
page_app.ItemSelectorForm = Backbone.View.extend({
tagName:"form",
template: _.template($("#tplFoodSelectorForm").html()),
initialize: function(){
this.results = {
'food_items': {},
'food_groups': {}
},
this.food_group_collection = new page_app.FoodGroupCollection();
this.food_item_collection = new page_app.FoodItemCollection();
},
render: function(){
var self = this;
if(typeof this.results.food_items.meta =='undefined' || this.results.food_items.meta.total < 1){
this.food_group_collection.fetch({
success: function(newCollection){
var current = newCollection.models[0];
if(current){
self.results.food_groups.meta = current.attributes.meta;
self.results.food_groups.objects = current.attributes.objects;
}
console.log(self.results.food_groups);
//self.$el.html(self.template({groups:self.results.food_groups.objects}));
}
});
}
this.results = self.results;
console.log(this.results.food_groups);
return this;
}
});
page_app.ItemSelectorModalView = Backbone.View.extend({
el: $("#ItemSelectorModal"),
initialize: function(){
this.$body = this.$el.find('.modal-body');
this.$form = new page_app.ItemSelectorForm();
this.render();
},
events: {
'shown.bs.modal': 'shown',
'hidden.bs.modal': 'hidden'
},
render: function(){
this.$body.html(this.$form.render().$el.html());
return this;
},
show: function(){
this.$el.modal('show');
},
hide: function(){
this.$el.modal('hide');
},
shown: function(e){
},
hidden: function(event){
event.preventDefault();
this.$el.removeData('bs.modal');
}
});
//Items Place Holder
page_app.ItemsPlaceholder = Backbone.View.extend({
el: "#ItemsPlaceHolder",
initialize: function(){
//this.form = new page_app.ItemSelectorForm();
},
events: {
"click #btnAddFoodItem": "modalOpen"
},
modalOpen : function(event){
event.preventDefault();
this.modal = new page_app.ItemSelectorModalView();
this.modal.show();
},
modalHidden: function(e){
console.log(e);
},
render: function(){
return this;
}
});
})(jQuery);
jQuery(function(){
//
var items_placeholder = new page_app.ItemsPlaceholder();
items_placeholder.render();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment