Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to pass model reference directly to HTML element?

Is it correct to pass model reference directly to HTML element?

window.car = {
   color: "blue",
   maxSpeed: 200
 }
var element = $("<div>");
car.$element = element;
element[0]._model = car;

$("") creates a new jQuery element. JQuery has wrapped elements saved inside in keys 0,1,2 ... so [0] return first wrapped HTMLelement. _model is only name of reference variable.

like image 325
Zdeněk Mlčoch Avatar asked Mar 19 '12 14:03

Zdeněk Mlčoch


2 Answers

Use jQuery's data() method for that. Adding new properties directly to DOM nodes may run into problems and is considered harmful.

like image 61
Bergi Avatar answered Oct 22 '22 14:10

Bergi


maybe you could attach the object to a data-model attribute instead of attaching it directly as a dom node property (it could be destructive), just using

$(element[0]).data('model', car);
like image 44
Fabrizio Calderan Avatar answered Oct 22 '22 14:10

Fabrizio Calderan