Skip to content

Commit

Permalink
Refactor sort method.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinRixham committed Nov 19, 2016
1 parent 7823a8c commit ca84c36
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ An opinionated data binding library.

The two components of a web page built with Datum.js are the *template* and the *view model*.
The template is the HTML between the `<body> </body>` tags before Datum.js has modified it.
You the developer are responsible for putting the tempate in the body of the DOM.
You the developer are responsible for putting the template in the body of the DOM.
Datum.js is agnostic about how you do this.
(If you're having trouble jQuery's [load](https://api.jquery.com/load/) method is a good option.)

Expand Down
2 changes: 1 addition & 1 deletion docs/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h2 id="the-basics-of-data-binding-with-datum">The Basics of Data Binding with D
<h3 id="initialisation">Initialisation</h3>
<p>The two components of a web page built with Datum.js are the <em>template</em> and the <em>view model</em>.
The template is the HTML between the <code>&lt;body&gt; &lt;/body&gt;</code> tags before Datum.js has modified it.
You the developer are responsible for putting the tempate in the body of the DOM.
You the developer are responsible for putting the template in the body of the DOM.
Datum.js is agnostic about how you do this.
(If you&#39;re having trouble jQuery&#39;s <a href="https://api.jquery.com/load/">load</a> method is a good option.)</p>
<p>The view model is a single JavaScript object that contains all of the data that will be displayed on the web page and all of the logic for interacting with the web page.
Expand Down
16 changes: 9 additions & 7 deletions src/array/ArrayElement.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
define(["array/ArrayItemElement"], function(ArrayItemElement) {

function ArrayElement(element, initialLength) {
function ArrayElement(domElement, initialLength) {

var element = domElement.get();

var child;

Expand Down Expand Up @@ -92,6 +94,11 @@ define(["array/ArrayItemElement"], function(ArrayItemElement) {
}
};

this.appendChild = function(child) {

element.appendChild(child);
};

this.reset = function() {

while (element.lastChild) {
Expand All @@ -102,11 +109,6 @@ define(["array/ArrayItemElement"], function(ArrayItemElement) {
element.appendChild(child.get());
};

this.getChild = function() {

return child;
};

this.equals = function(other) {

return other.hasEqual(element);
Expand All @@ -119,7 +121,7 @@ define(["array/ArrayItemElement"], function(ArrayItemElement) {

this.get = function() {

return element;
return domElement;
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/array/ArrayItemElement.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define([], function() {

function ArrayElement(element) {
function ArrayItemElement(element) {

var index = 0;

Expand Down Expand Up @@ -42,5 +42,5 @@ define([], function() {
};
}

return ArrayElement;
return ArrayItemElement;
});
10 changes: 2 additions & 8 deletions src/array/method/Push.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
define([
"property/TransientProperty",
"element/DOMElement"
], function(
TransientProperty,
DOMElement) {
define(["property/TransientProperty"], function(TransientProperty) {

function Push(model, elements, properties, propertyType) {

Expand All @@ -25,8 +20,7 @@ define([
var finalIndex = properties.length - 1;

element.append();
property.applyBinding(
new DOMElement(element.get()), finalIndex, model);
property.applyBinding(element.get(), finalIndex, model);
}
}

Expand Down
17 changes: 2 additions & 15 deletions src/array/method/Sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ define([], function() {

for (var i = 0; i < elements.length; i++) {

var element = elements[i].get();
var children = [].slice.call(element.children);

removeChildren(element);
var children = elements[i].removeChildren();

for (var j = 0; j < children.length; j++) {

Expand All @@ -57,14 +54,6 @@ define([], function() {
return allChildren;
}

function removeChildren(element) {

while (element.lastChild) {

element.removeChild(element.lastChild);
}
}

function replaceSortedObjects(modelChildrenProperties) {

for (var i = 0; i < model.length; i++) {
Expand All @@ -74,9 +63,7 @@ define([], function() {

for (var j = 0; j < elements.length; j++) {

var element = elements[j].get();

element.appendChild(modelChildrenProperties[i].children[j]);
elements[j].appendChild(modelChildrenProperties[i].children[j]);
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/array/method/Splice.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
define([
"property/TransientProperty",
"element/DOMElement"
], function(
TransientProperty,
DOMElement) {
define(["property/TransientProperty"], function(TransientProperty) {

function Splice(model, elements, properties, propertyType) {

Expand Down Expand Up @@ -63,7 +58,7 @@ define([
var element = elements[j];

element.insertAtIndex(start);
property.applyBinding(new DOMElement(element.get()), start, model);
property.applyBinding(element.get(), start, model);
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/array/method/Unshift.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
define([
"property/TransientProperty",
"element/DOMElement"
], function(
TransientProperty,
DOMElement) {
define(["property/TransientProperty"], function(TransientProperty) {

function Unshift(model, elements, properties, propertyType) {

Expand All @@ -24,7 +19,7 @@ define([
var element = elements[j];

element.prepend();
property.applyBinding(new DOMElement(element.get()), 0, model);
property.applyBinding(element.get(), 0, model);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/element/DOMElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ define([

this.toArrayElement = function(initialLength) {

return new ArrayElement(element, initialLength);
return new ArrayElement(this, initialLength);
};

this.get = function() {
Expand Down

0 comments on commit ca84c36

Please sign in to comment.