Skip to content
Browse files

Added support for simple arrays as collections.

  • Loading branch information...
1 parent d93999d commit 148f091c2c87b8676e7ccbd9723aad7ec012fb34 @adscott committed Apr 25, 2012
Showing with 39 additions and 17 deletions.
  1. +29 −15 meninges-view.js
  2. +10 −2 spec/meninges-view-spec.js
View
44 meninges-view.js
@@ -1,6 +1,18 @@
Backbone.MeningesView = {
extend: function (o) {
+ var coerceType = function (originalValue, value) {
+ if (_(originalValue).isNumber()) {
+ var parsedValue = parseFloat(value);
+ value = _(parsedValue).isNaN() ? value : parsedValue;
+ value = value === '' ? null : value;
+ }
+ if (value === '' && originalValue === null) {
+ value = null;
+ }
+ return value;
+ }
+
var findNextModel = function (startingModel, pathElement) {
if (pathElement.indexOf(":") === -1) {
return startingModel.get(pathElement);
@@ -35,22 +47,24 @@ Backbone.MeningesView = {
originalModel = findNextModel(originalModel, pathItems[i]);
}
- var newValueHash = {};
- var oldValue = currentModel.get(_(pathItems).last());
- var originalValue = originalModel ? originalModel.get(_(pathItems).last()) : null;
- if (_(originalValue).isNumber()) {
- var parsedValue = parseFloat(value);
- value = _(parsedValue).isNaN() ? value : parsedValue;
- value = value === '' ? null : value;
- }
- if (value === '' && originalValue === null) {
- value = null;
- }
+ var key = _(pathItems).last();
+
+ if (key.indexOf(":") === -1) {
+ var newValueHash = {};
+ var oldValue = currentModel.get(key);
+ var originalValue = originalModel ? originalModel.get(_(pathItems).last()) : null;
+ var value = coerceType(originalValue, value);
- var sameValue = (oldValue === value);
- newValueHash[_(pathItems).last()] = value;
- if (!sameValue) {
- currentModel.set(newValueHash);
+ var sameValue = (oldValue === value);
+ newValueHash[_(pathItems).last()] = value;
+ if (!sameValue) {
+ currentModel.set(newValueHash);
+ }
+ } else {
+ var elements = key.split(":");
+ var collection = currentModel.get(elements[0]);
+ var index = elements[1];
+ collection[index] = coerceType(collection[index], value);
}
};
View
12 spec/meninges-view-spec.js
@@ -20,7 +20,8 @@ describe("meninges views", function () {
links: [
{type: "buy", uri: "http://amazon.fr/123"},
{type: "read", uri: "http://livresenligne.fr/lemenon"}
- ]
+ ],
+ publishers: ["Penguin", "Random House"]
};
};
@@ -69,7 +70,9 @@ describe("meninges views", function () {
'<option value="europe">europe</option><option value="afrique">afrique</option></select>' +
'<input id="buy" name="links:0.type" class="meninges" type="radio" value="buy" checked="checked" /> Buy' +
'<input id="read" name="links:0.type" class="meninges" type="radio" value="read" /> Read' +
- '<input name="author.is_dead" class="meninges" type="checkbox" />';
+ '<input name="author.is_dead" class="meninges" type="checkbox" />' +
+ '<input name="publishers:0" class="meninges" type="text" />';
+ '<input name="publishers:1" class="meninges" type="text" />';
$(this.el).html(html);
$("#book-form-container").html(this.el);
}
@@ -232,6 +235,11 @@ describe("meninges views", function () {
$($("input[name='links:0.type']")[1]).click();
expect(book.get("links").at(0).get("type")).toEqual("read");
});
+
+ it("should synchronise arrays on models", function () {
+ $("input[name='publishers:0']").val("My New Publisher").trigger("blur");
+ expect(book.get("publishers")[0]).toEqual("My New Publisher");
+ });
});
describe("boolean values (checkboxes)", function () {

0 comments on commit 148f091

Please sign in to comment.
Something went wrong with that request. Please try again.