it('should keep items with duplicated IDs if Collection.allowDuplicatedIds flag is set to true', function () { var TCollection = Collection.extend({ allowDuplicatedIds: true }); var items = [{id: 4, color: '#efefef', primary: false}, {id: 3, color: 'transparent', primary: true}]; var testCollection; [].push.apply(colors, items); testCollection = new TCollection(colors); expect(testCollection.count()).toEqual(colors.length); expect(testCollection.toObject().items).toEqual(colors); });
it('can add models with a custom model type', function() { var colorModel = Model.extend({ isBlack: function() { return this.get('color') === 'black'; } }), colorCollection = Collection.extend({ TModel: colorModel }); testCollection = new colorCollection(colors); expect(testCollection.models[0] instanceof colorModel).toEqual(true); });
define(function(require) { var Collection = require('lavaca/mvc/Collection'), debounce = require('mout/function/debounce'), individualJobModel = require('app/models/individualJobModel'), stateModel = require('app/models/StateModel'); var JobCollection = Collection.extend(function JobCollection() { Collection.apply(this, arguments); }, { TModel: individualJobModel, search:function() { IN.API.Raw("/people/~/suggestions/job-suggestions").result( function(me) { console.log(me.jobs.values[0]) }.bind(this)); } }); // Computed properties return JobCollection; });
define(function(require) { var Collection = require('lavaca/mvc/Collection'); var port = require('panel/net/port'); var ViewsCollection = Collection.extend(function() { Collection.apply(this, arguments); this.apply({ viewTree: this.buildViewTree }); }, { buildViewTree: function() { var viewsWithParentIndexes = []; this.each(function(i, model) { var copyModel = model.toObject(), parentView = model.get('parentView'), parentIndex = -1; copyModel.model = JSON.stringify(copyModel.model || {}, undefined, 2); if (parentView) { this.models.some(function(item, i) { if (parentView === item.get('id')) { parentIndex = i; return true; } }); } viewsWithParentIndexes.push([ copyModel, parentIndex ]); }.bind(this)); return makeTree(viewsWithParentIndexes); } }); function makeTree(arr){ //Array with all the children elements set correctly.. var treeArr = []; var depth = 0; var lastParent; for (var i = 0, len = arr.length; i < len; i++){ var arrI = arr[i]; var newNode = { name: arrI[0], children: [] }; var found; var parentI = arrI[1]; if (parentI > -1){ //i.e. not the root.. found = recursiveSearchById(treeArr, arr[parentI][0].id); depth = found.depth + 1; newNode.depth = depth; found.children.push(newNode); } else { depth = newNode.depth = 0; treeArr.push(newNode); } lastParent = parentI; } return treeArr; //return the root.. } function recursiveSearchById(items, id) { if (items) { for (var i = 0; i < items.length; i++) { if (items[i].name.id == id) { return items[i]; } var found = recursiveSearchById(items[i].children, id); if (found) return found; } } } return new ViewsCollection(); });
define(function(require) { var Collection = require('lavaca/mvc/Collection'); var BookModel = require('app/models/BookModel'); var stateModel = require('app/models/StateModel'); var debounce = require('mout/function/debounce'); var FavoriteCollection = Collection.extend(function() { Collection.apply(this, arguments); stateModel.on('favorite:add', this.addFavorite, this); stateModel.on('favorite:remove', this.removeFavorite, this); stateModel.on('favorite:search', this.search, this); if (localStorage.getItem('books:favorites')) { var obj = JSON.parse(localStorage.getItem('books:favorites')); this.add(obj[this.itemsProperty]); } this.on('addItem', debounce(this.storeData, 300), this); this.on('removeItem', debounce(this.storeData, 300), this); },{ /** * @field {Object} TModel * @default [[BookCollection]] * The type of model object to use for items in this collection */ TModel: BookModel, /** * @field {String} itemsProperty * @default 'books' * The name of the property containing the collection's items when using toObject() */ itemsProperty: 'books', addFavorite: function (e) { this.add(e.model); }, removeFavorite: function (e) { this.remove(e.model); }, storeData: function(e) { localStorage.setItem('books:favorites', JSON.stringify(this.toObject())); }, search: function (e) { var model = this.first({id: e.id}); stateModel.trigger('favorite:searchResult', { isFavorite: model ? true : false }); }, dispose: function () { stateModel.off('favorite:add', this.addFavorite, this); stateModel.off('favorite:remove', this.removeFavorite, this); stateModel.off('favorite:search', this.search, this); return Collection.prototype.dispose.apply(this, arguments); } }); return new FavoriteCollection(); });
define(function (require) { 'use strict'; // Constants var LOCAL_STORAGE_KEY = 'todos-lavaca-require'; var Collection = require('lavaca/mvc/Collection'); /** * A collection of ToDo items that saves and restores its data from * localStorage * @class app.models.TodosCollection * @super Lavaca.mvc.Collection */ var TodosCollection = Collection.extend(function TodosCollection() { // Call the super class' constructor Collection.apply(this, arguments); // Set some computed properties on this model this.apply({ allComplete: allComplete, itemsLeft: itemsLeft, itemsCompleted: itemsCompleted }); // Restore any data from localStorage restore.call(this); // Listen for changes to the models and then save them in localStorage this.on('addItem', store); this.on('moveItem', store); this.on('removeItem', store); this.on('changeItem', store); }, { /** * @method removeCompleted * Remove models that are complete */ removeCompleted: function () { this.remove({completed: true}); } }); /* ---- Computed Properties ---- */ // Returns a boolean indicating whether all items are complete function allComplete() { var allAreComplete = true; this.each(function (index, model) { if (!model.get('completed')) { allAreComplete = false; return false; // break out of `each` loop } }); return allAreComplete; } // Returns a count of incomplete items function itemsLeft() { return this.filter({ completed: false }).length; } // Returns a count of complete items function itemsCompleted() { return this.filter({ completed: true }).length; } /* ---- Handle Persistence ---- */ // Called every time the models change. Set a timeout that will write the data // to localStorage. Clear the timeout with every call to make sure that data // is only written once even if multiple changes are made in the same run loop function store() { var items; clearTimeout(this.storeTimer); this.storeTimer = setTimeout(function () { // Safari will throw an exception when trying to write to localStorage in // private browsing mode try { items = JSON.stringify(this.toObject().items); localStorage.setItem(LOCAL_STORAGE_KEY, items); } catch (e) {} }.bind(this)); } // Pull the data from localStorage and add it to the collection function restore() { var data; var i; try { data = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY)); if (data) { for (i = 0; i < data.length; i++) { this.add(data[i]); } } } catch (e) {} } return new TodosCollection(); });