Skip to content

Commit

Permalink
connect using separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
corinis committed Dec 18, 2013
1 parent 2ab9786 commit d6cba5e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
3 changes: 2 additions & 1 deletion sample-connect.html
Expand Up @@ -18,8 +18,9 @@
// initialize the form, prefix is optional and defaults to data
$("#details").jsForm({
data:jsonData,
connect: [$("#additional")]
});

$(details).jsForm("connect", $("#additional"));

$("#show").click(function() {
// show the json data
Expand Down
51 changes: 43 additions & 8 deletions src/jquery.jsForm.js
Expand Up @@ -99,6 +99,29 @@
this._fill(this.options);
};

/**
* Connect a dom element with an already existing form.
* @param ele the new part of the form
*/
JsForm.prototype.connect = function(ele) {
// collection lists with buttons
this._initCollection(ele, this.options.prefix);
// init conditionals
this._initConditional(ele, this.options.prefix, this.options);

// enable form controls
if(this.options.controls) {
if($.jsFormControls) {
// handle multiple form parts
$(ele).jsFormControls();
}
}

this._fillDom(ele);
if(!this.options.connect)
this.options.connect = [];
this.options.connect.push(ele);
};

/**
* init the dom. This can be called multiple times.
Expand Down Expand Up @@ -1297,16 +1320,28 @@
$(this.element).data("pojo", this.options.data);

// handle multiple form parts
$.each(that._getForm(), function(){
that._clear(this, that.options.prefix);

// fill base
that._fillData(this, that.options.data, that.options.prefix);
that._fillCollection(this, that.options.data, that.options.prefix);
// (re-)evaluate all conditionals
that._evaluateConditionals(this, that.options.data);
$.each(this._getForm(), function(){
that._fillDom(this);
});
};

/**
* This is the actual worker function that fills a dom element
* with data.
* @param ele the element to fill
* @private
*/
JsForm.prototype._fillDom = function(ele) {
var that = this;
that._clear(ele, that.options.prefix);

// fill base
that._fillData(ele, that.options.data, that.options.prefix);
that._fillCollection(ele, that.options.data, that.options.prefix);
// (re-)evaluate all conditionals
that._evaluateConditionals(ele, that.options.data);
};


/**
* @param container the container element
Expand Down

0 comments on commit d6cba5e

Please sign in to comment.