arturadib / agility

Javascript MVC for the "write less, do more" programmer
http://agilityjs.com
MIT License
542 stars 70 forks source link

Bind Agility Object to DOM element in page #114

Open mahadevan-k opened 10 years ago

mahadevan-k commented 10 years ago

Would be awesome if I could bind an agility object to an existing element on the page. e.g.

var menu=$$({},$('.menu'),{}) ;

How can we achieve this?

gaby64 commented 10 years ago

change render in view object to

render: function(){ if(typeof this.view.format != 'string') { this.view.$root = this.view.format; this.view.format = this.view.format.html(); } else if (this.view.$root.size() === 0) { this.view.$root = $(this.view.format); } else { this.view.$root.html( $(this.view.format).html() ); // can't overwrite $root as this would reset its presence in the DOM and all events already bound, and } return this; }

and change

if (typeof args[1] === 'object') { if(args[1].format) $.extend(object.view, args[1]); else object.view.format = args[1]; }

mahadevan-k commented 10 years ago

k, some basic stuff seem to work after a tweak to handle empty objects passed as the 'view' argument in the $$ constructors.

In render function:

if(typeof this.view.format != 'string' && !$.isEmptyObject(this.view.format)) { this.view.$root = this.view.format; this.view.format = this.view.format.html(); } else if (this.view.$root.size() === 0) { this.view.$root = $(this.view.format); }

and

if (typeof args[1] === 'object') { if(args[1].format || $.isEmptyObject(args[1])) $.extend(object.view, args[1]); else object.view.format = args[1]; }

Will check more thoroughly as I implement code using the tweak.

mahadevan-k commented 10 years ago

The view binding works great.

But if I did something like

<div class="agility article"><p data-bind="content">Lorem Ipsum</p>

And create the agility object using the above code, the model doesn't reflect the values, i.e.

article.model.get("content") is undefined

Is there a way to load the model from the DOM when the object is initialized?