From 8c096d65b4734872c6475298008dadb60c703481 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 20 May 2013 10:42:03 +0200 Subject: [PATCH 1/2] basic get/setModel implemented --- component.json | 3 ++- index.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/component.json b/component.json index 7d3940c..989325d 100644 --- a/component.json +++ b/component.json @@ -10,7 +10,8 @@ "component/domify": "*", "component/type": "*", "visionmedia/minstache": "*", - "nickjackson/val": "*" + "nickjackson/val": "*", + "component/model": "*" }, "development": {}, "license": "MIT", diff --git a/index.js b/index.js index 10a31b9..bf196f2 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,24 @@ Form.prototype.getValue = function(){ return values; } +/** + * Iterate through all values and updates the model + * + * @param {Model} model + * @return {Form} this + * @api public + */ + +Form.prototype.getModel = function(model){ + if (this.model === undefined) + return; + var values = this.getValue(); + for (var attr in values) { + this.model[attr](values[attr]); + } + return this.model; +} + /** * Iterates through all attributes and * set their values; @@ -90,4 +108,17 @@ Form.prototype.setValue = function(data){ values[attr] = attribute.value(data[attr]); } return values; +} + +/** + * Sets the values of model + * + * @param {Model} model + * @return {Form} this + * @api public + */ + +Form.prototype.setModel = function(model){ + this.model = model; + return this.setValue(model.attrs); } \ No newline at end of file From 01ecd44fd690c7f2a15ec44cd35f81114e0c299a Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 20 May 2013 10:45:03 +0200 Subject: [PATCH 2/2] basic get/setModel implemented --- Readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Readme.md b/Readme.md index 3fc6608..15fa80d 100644 --- a/Readme.md +++ b/Readme.md @@ -29,6 +29,12 @@ Fills in rendered form with data given ### form.getValue() Returns object of current values + +### form.setModel(model) +Takes a component/model instance and calls ``setValue()``. + +### form.getModel() +Returns the updated model instance. ### form.view Rendered DOM