Created
November 7, 2011 17:55
-
-
Save boxxxie/1345664 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var install_db = db('install'); | |
| var Selection = new (Backbone.Model.extend()); | |
| var Company = couchDoc.extend( | |
| {defaults: function() { | |
| return { | |
| name:"unknown", | |
| hierarchy:{groups:["none"]} | |
| }; | |
| }, | |
| addGroup: function(groupToAdd){ | |
| var oldHierarchy = this.get('hierarchy'); | |
| var groups = oldHierarchy.groups; | |
| if(!_(groups).chain().contains(groupToAdd).value()){ | |
| var newHierarchy = oldHierarchy.groups.concat(groupToAdd); | |
| this.set({hierarchy:newHierarchy}); | |
| this.save(); | |
| } | |
| }, | |
| addStore: function(group,store){}, | |
| addTerminal: function(group,store,terminal){} | |
| }); | |
| var Companies; | |
| var companiesView; | |
| var companiesViewTest; | |
| var groupsView; | |
| var groupsViewTest; | |
| //may not need this | |
| function multiselectClick(caller,checkbox){ | |
| // $(caller).target().id() | |
| var callerID = caller.target.id; | |
| var checkboxName = checkbox.value; | |
| switch(callerID){ | |
| case "companies": | |
| Selection.set({'company':checkboxName}); | |
| break; | |
| } | |
| console.log("click"); | |
| } | |
| var regionSelectorSettings = { | |
| minWidth:700, | |
| selectedList: 6, | |
| multiple: false, | |
| //the below would fire off events for backbone to take care of | |
| click: multiselectClick, | |
| /* checkAll:multiselectRefresh, | |
| uncheckAll:multiselectRefresh, | |
| optgroupToggle:multiselectRefresh, | |
| refresh: multiselectRefresh,*/ | |
| position: { | |
| my: 'left bottom', | |
| at: 'left top' | |
| } | |
| }; | |
| function genericButtonSetup($node,clickCallback){ | |
| $node.click(clickCallback); | |
| }; | |
| function addCompany(collection){ | |
| return function(){ | |
| var input = window.prompt("Enter New Company Name",""); | |
| if(!input || | |
| input == "" || | |
| collection.chain().pluck('name').contains(input).value()) | |
| {return;} | |
| collection.create({name:input}); | |
| }; | |
| }; | |
| function addGroup(model){ | |
| return function(){ | |
| //get the model that is represented in the select box | |
| var input = window.prompt("Enter New Group Name",""); | |
| if(!input || input == "")return; | |
| model.addGroup(input); | |
| }; | |
| }; | |
| function doc_setup(){ | |
| Companies = | |
| new (couchCollection({db:'install'}, | |
| {model:Company, | |
| getModelByName : function(modelName){ | |
| return this.find(function(model){return model.get('name') == modelName;}); | |
| }, | |
| getSelectedModel : function(){ | |
| return this.find(function(model){return model.selected == true;}); | |
| } | |
| })); | |
| Companies.fetch(); | |
| genericButtonSetup($("#btnAddCompany"), addCompany(Companies)); | |
| genericButtonSetup($("#btnAddGroup"), function(){ | |
| return function(collection){ | |
| var modelName = $("#companies").multiselect('getChecked').val(); | |
| var model = collection.getModelByName(modelName); | |
| addGroup(model); | |
| }(Companies); | |
| }); | |
| // genericButtonSetup($("#btnAddStore")); | |
| // genericButtonSetup($("#btnAddTerminal")); | |
| companiesView = Backbone.View.extend( | |
| {initialize:function(){ | |
| var view = this; | |
| _.bindAll(view, 'render'); | |
| this.collection.bind('reset',view.render); | |
| this.collection.bind('change',view.render); | |
| this.collection.bind('add',view.render); | |
| $(this.el).multiselect(_.extend(regionSelectorSettings,{noneSelectedText:"Companies"})); | |
| }, | |
| render:function(){ | |
| var html = ich.options_TMP1({list:this.collection.toJSON()}); | |
| $(this.el).html(html); | |
| $(this.el).multiselect("refresh"); | |
| console.log("companies view rendered"); | |
| return this; | |
| } | |
| }); | |
| companiesViewTest = new companiesView( | |
| { | |
| collection: Companies, | |
| el:_.first($("#companies")), | |
| selector:Selection | |
| }); | |
| groupsView = Backbone.View.extend( | |
| {initialize:function(){ | |
| var view = this; | |
| _.bindAll(view, 'render'); | |
| // this.collection.bind('change',view.render); | |
| Selection.bind('change:company',function(){console.log('change:company');view.render();}); | |
| $(this.el).multiselect(_.extend(regionSelectorSettings,{ noneSelectedText:"Groups"})); | |
| }, | |
| render:function(){ | |
| var html = ich.options_TMP1({list:this.collection.toJSON()}); | |
| $(this.el).html(html); | |
| $(this.el).multiselect("refresh"); | |
| console.log("groups view rendered"); | |
| return this; | |
| } | |
| }); | |
| groupsViewTest = new companiesView( | |
| { | |
| model: Companies, | |
| el:_.first($("#groups")) | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment