Skip to content
This repository has been archived by the owner on May 2, 2019. It is now read-only.

Set/Get model #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"component/domify": "*",
"component/type": "*",
"visionmedia/minstache": "*",
"nickjackson/val": "*"
"nickjackson/val": "*",
"component/model": "*"
},
"development": {},
"license": "MIT",
Expand Down
31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}