ampersand-view
Advanced tools
Comparing version 5.1.2 to 5.1.3
var State = require('ampersand-state'); | ||
var Events = require('backbone-events-standalone'); | ||
var CollectionView = require('ampersand-collection-view'); | ||
var domify = require('domify'); | ||
@@ -341,70 +342,11 @@ var _ = require('underscore'); | ||
renderCollection: function (collection, ViewClass, container, opts) { | ||
var self = this; | ||
var views = []; | ||
var options = _.defaults(opts || {}, { | ||
filter: null, | ||
viewOptions: {}, | ||
reverse: false | ||
}); | ||
container = (typeof container === 'string') ? this.get(container) : container; | ||
// store a reference on the view to it's collection views | ||
// so we can clean up memory references when we're done | ||
this.registerSubview(views); | ||
function getViewBy(model) { | ||
return _.find(views, function (view) { | ||
return model === view.model; | ||
}); | ||
} | ||
function addView(model, collection, opts) { | ||
var matches = options.filter ? options.filter(model) : true; | ||
var view; | ||
if (matches) { | ||
view = getViewBy(model); | ||
if (!view) { | ||
view = new ViewClass(_({model: model, collection: collection}).extend(options.viewOptions)); | ||
views.push(view); | ||
view.parent = self; | ||
view.renderedByParentView = true; | ||
if (!view.rendered) view.render({containerEl: container}); | ||
} | ||
// give the option for the view to choose where it's inserted if you so choose | ||
if (!view.insertSelf) { | ||
if (options.reverse) { | ||
container.insertBefore(view.el, container.firstChild); | ||
} else { | ||
container.appendChild(view.el); | ||
} | ||
} | ||
view.delegateEvents(); | ||
} | ||
} | ||
function reRender() { | ||
// empty without using jQuery's empty (which removes jQuery handlers) | ||
container.innerHTML = ''; | ||
collection.each(function (model) { | ||
addView(model); | ||
}); | ||
} | ||
this.listenTo(collection, 'add', addView); | ||
this.listenTo(collection, 'remove', function (model) { | ||
var index = views.indexOf(getViewBy(model)); | ||
if (index !== -1) { | ||
// remove it if we found it calling animateRemove | ||
// to give user option of gracefully destroying. | ||
views.splice(index, 1)[0].animateRemove(); | ||
} | ||
}); | ||
this.listenTo(collection, 'move sort', reRender); | ||
this.listenTo(collection, 'refresh reset', function () { | ||
// empty array calling `remove` on each | ||
// without re-defining `views` | ||
while (views.length) { | ||
views.pop().remove(); | ||
} | ||
reRender(); | ||
}); | ||
reRender(); | ||
var containerEl = (typeof container === 'string') ? this.get(container) : container; | ||
var config = _.extend({ | ||
collection: collection, | ||
el: containerEl, | ||
view: ViewClass | ||
}, opts); | ||
var collectionView = new CollectionView(config); | ||
collectionView.render(); | ||
this.registerSubview(collectionView); | ||
} | ||
@@ -411,0 +353,0 @@ }); |
{ | ||
"name": "ampersand-view", | ||
"description": "A smart base view for Backbone apps, to make it easy to bind collections and properties to the DOM.", | ||
"version": "5.1.2", | ||
"version": "5.1.3", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -18,3 +18,4 @@ "browser": "./ampersand-view.js", | ||
"matches-selector": "1.x.x", | ||
"underscore": "1.x.x" | ||
"underscore": "1.x.x", | ||
"ampersand-collection-view": "~1.0.0" | ||
}, | ||
@@ -45,3 +46,4 @@ "devDependencies": { | ||
"validate": "jshint .", | ||
"start": "run-browser test/index.js" | ||
"start": "run-browser test/index.js", | ||
"test": "browserify test/index.js | tape-run" | ||
}, | ||
@@ -48,0 +50,0 @@ "testling": { |
require('./main'); | ||
//require('./renderCollection'); | ||
require('./renderCollection'); |
@@ -191,2 +191,3 @@ var test = require('tape'); | ||
t.ok(extraInfo.containerEl); | ||
this.renderWithTemplate(); | ||
}; | ||
@@ -193,0 +194,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53061
11
1075
+ Addedampersand-class-extend@1.0.2(transitive)
+ Addedampersand-collection-view@1.0.11(transitive)
+ Addedlodash._baseassign@3.2.0(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._bindcallback@3.0.1(transitive)
+ Addedlodash._createassigner@3.1.1(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash.assign@3.2.0(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedlodash.restparam@3.6.1(transitive)