Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 2, 2016 01:00 pm

Backbone Views and the DOM: Part 2


file


In the last article, we covered the basics of Backbone Views and how to enable jQuery to seamlessly manipulate the same DOM that Backbone manipulates.



In this article, we will explore how we can make Backbone play nicely with d3.js. The concepts in this article should apply to situations where you intend to use Backbone with most other libraries that also manipulate the DOM.



Backbone + d3.js Working on the Same DOM



d3.js, written by Mike Bostock, is another widely used library that manipulates the Document Object Model, primarily to visualize data. In fact, you can get really creative with the way data can be visualized.



At the lowest level, d3.js interacts with HTML and/or SVG elements and manipulates their attributes by binding data to the Document Object Model.



Here is a quick example of how d3 operates:





The above code does the following:




  1. Selects and creates a reference to the body element

  2. Within that selection, selects all of the p elements currently in the DOM

  3. Appends each element in numericalData to a selected p element

  4. For each p element that has yet to exist (i.e., some elements in numericalData that still need to be appended), creates a p element and adds it the DOM

  5. Sets the text node in each newly created p element to contain some text (including the relevant number in numericalData)



A First Attempt to Make Backbone and d3.js Play Nicely



Leveraging what we learned in the previous article, here is one implementation of a shared DOM manipulation.





Houston, We’ve Got a Problem!



Assuming that our goal is to preserve the existing p element and append the other p elements to DOM, when we execute the above code, we quickly run into a major problem.



The .render() inserts a p element with the text “I am not a number” into the DOM. But .renderVisualization() selects all existing p elements in the DOM and inserts content into those elements. This overwrites the text in the original p element, despite our appending to the DOM using d3.append().



Solutions to Getting d3.js and Backbone to Play Nicely Together



In this simple example, at least two simple solutions exist.




  1. Use a more specific CSS selector

  2. Use a different tag altogether



Cordoning Off a Portion of the DOM



For more complicated examples, we may need alternative strategies. One potential solution is to cordon off a portion of the DOM that will be operated on by one of the libraries. For example:





In the above case, Backbone continues to manage the to-be-created view. However, let’s say that there is an element somewhere in the DOM (inside or outside of the DOM managed by the Backbone view) whose id is “visualization”. We can capitalize on this fact by scoping all of our d3-related DOM manipulations to this element. As a result, all d3 chained methods, including .selectAll("p") and .append("p"), are executed in the context of #visualization.



Encapsulate Differentiated DOM Manipulation With Sub-Views



Finally, another approach to managing third-party view functionality is to use subviews. Here is how that might look.





In this scenario, subViewA might contain non-visualization content, and subViewB might contain visualization content.



Another Shared-DOM Manipulation Approach for Backbone + d3.js



There are times when you need to make sure that your d3 DOM manipulation occurs in the same context as the Backbone view itself. For example, simply cordoning off a portion of the DOM with #visualization provides no guarantee that the element exists within or outside of the part of the DOM represented by the Backbone view.



One alternative is to ensure that your starting d3 selection references the same element as the one pointed to by this.$el. However, if you tend not to set el or any of its attributes directly, it would be difficult to sufficiently target the current view using CSS.



Luckily, in the d3.js library, there exists a method that enables direct node selection. Let me introduce you to d3.select(node). According to the documentation, this method:




Selects the specified node. This is useful if you already have a reference to a node, such as d3.select(this) within an event listener, or a global such as document.body. This function does not traverse the DOM.




This method allows us to write the following code and ensure that d3 manipulations occur within the context of the Backbone view.





Conclusion



In summary, there are a number of considerations when making Backbone play nicely with other DOM manipulators.




  1. Be sure to select the right element to manipulate. Using specific CSS classes and ids can make life much easier.

  2. Clever uses of the various DOM manipulation methods in either library can go a long way. Be sure to clarify the DOM action (append, prepend, insert, remove) you really need.

  3. Looping operations in the DOM can be tricky. Ensure that the correct elements are impacted.



There are also a number of alternatives to consider, depending on the outcome you seek:




  1. Separating out a portion of the DOM

  2. Scoping the other library’s DOM manipulation to the Backbone context

  3. Using sub-views to encapsulate the impact of other DOM manipulators



Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code