henrikjoreteg / human-view Goto Github PK
View Code? Open in Web Editor NEWA smart base view for Backbone apps, to make it easy to bind collections and properties to the DOM.
A smart base view for Backbone apps, to make it easy to bind collections and properties to the DOM.
What does this mean, and how/why do I use it?
- A pattern for easily including the view's base element into render. Rather than having to specify tag type and attributes in javascript in the view definition you can just include that in your template like everything else.
Using the events collection to bind events to the view actually binds the events to any element in the DOM that matches the selector passed in.
Instead, the events should bind only to children of the view's $el.
Bare with me, this is not easy or simple to explain.
The array you create called "views" on line 228 which is then used to store "copies" of the rendered views on line 253 is causing problems for Internet Explorer 10.
The problem is, IE10 maintains a reference between the original view and the "copy" of the view stored in views[], so when you set containerEl[0].innerHTML = '' on line 265, IE10 also wipes the innerHTML of the "copy" of the view which was stored in views[]. (All other browsers leave the innerHTML of the view stored in views[] intact.)
Then, when addView() is called for each view in reRender(), say, when a new model is added to the collection via create(), containerEl tries to append the retrieved view (from line 250) to itself on line 259, but the view.el's innerHTML is blank at that point because of line 265.
This causes all views to disappear when a new model is added to a collection and the views are reRender()ed. You can test this behavior in IE10 by doing the following:
function reRender() {
// empty without using jQuery's empty (which removes jQuery handlers)
for (var i = 0, l = views.length; i < l; i++){
console.log(views[i].el.innerHTML);
}
containerEl[0].innerHTML = '';
for (var i = 0, l = views.length; i < l; i++){
console.log(views[i].el.innerHTML);
}
collection.each(addView);
}
Looping the views[] array before and after the containerEl clears its HTML gives you a different result in IE10 vs any other browser. In IE10, the views in views[] all have correct innerHTML before containerEl clears its own innerHTML, and then empty innerHTML after. In all other browsers, the innerHTML in the views[] array is correct both before and after.
I tried several solutions to this bug, but have not solved it. I attempted to put a bandaid on the problem with my pull request #19, but that caused unexpected problems with event bindings. One solution was to pass the view through JSON.stringify then JSON.parse to create a true "copy" of the view to store in the views[] array, but while IE10 was OK with that, the other browsers choked while trying to stringify a circular ref inside the view object. Other attempts to create true copies of the view object to store in the views[] array also failed.
Somehow, the copy of the view stored in views[] needs to be "detached" from the original view object, otherwise IE10 will always destroy the innerHTML of all the views that get passed through reRender(), leaving blank markup on the page.
A fix for this would remove the need for PR #19, which is now clearly not the correct solution.
We currently do something like this for renderCollection
: https://github.com/HenrikJoreteg/human-view/blob/master/human-view.js#L193-L194
But this could be more generic and let you use it for simple subviews too.
Haven't dug into it completely, but as far as I can tell we only register subviews on the initial collection render (https://github.com/HenrikJoreteg/human-view/blob/master/human-view.js#L215) any collection views added later won't be added to that list.
I'd like to have AMD compatibility. Both for importing Backbone and exporting the module. Would you accept a pull request?
Or use this.$ instead?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.