Saturday 15 May 2010

javascript - binding variable value to views or specific section/div of html page? -


Every time a click is done, a button dynamically creates a div element within my single page app. I want to bind each new div element with the timestamp I created.

When buttons are clicked in these div elements, they should alert timestamps created.

My code is something like this

  click on $ ("#creatediv".) (Function () {var n = new Date () getTime (); $ ("# Containerdiv") attached (. "
gt; & gt; button & gt; & lt; / div & gt; & lt; / div & gt; ");}); $ ("Button.showememestamp"). Click (function () {warning ("timestamp is value");});

The button nesting level is not specific, so I can not do anything

  $ (this). Parent (). Parent (). Attr ("Id") partition ('-') [0];  

Question:

  1. How can I do the specific element and the bind value of a variable in its children?
    If I want to change the value of a specific variable then it should be changed only in that element

  2. Do I need to use a global object Which stores the data and passes the indicator of that data into the ID of all children within the newly created divis?

  3. Or what data binding framework would be better such as angular.js or backbone? If so, how should it be done?

      $ ("# Creatediv"). Click (function () {jquery} .data ()  Methods to add metadata to methods. N = new Date () GetTime (); on div = $ ("& lt; div id Div.append (extrastuff); div.append ($ ("(& lt Click the button & gt; Timestamp & lt; / button & gt; ") .data (" parentDiv ", div) .click (function (EV) {var parentDiv = $ (this). Data ('parent'); Var createdTimestamp = parentdevdataata ('createdtimestamp'); warning ("timestamp value" + createdTimestamp);}));});   

    Note that in my example, you can technically see all the settings and statistics using the .data () call, and instead only n variable directly in the .click () event handler. If you do this, it will be an example of javascript closing. But the example I wrote is .data () .

    The use of UPDATE

    is high. In the example above, to use the closure, edit the .data () by using jQuery:

      $ ("# creatediv"). Click on (function. () {Var n = new Date () getTime (); div div = $ ("& lt; div id =" + n + "- postfixtext '& gt; & lt; / div & gt; "); Div.append (extrastuff); Div.append ($ (" click for button & gt; timestamp  "). Click (function (ev) {warning ( "Timestamp value" + n);}));});   

    You can reference the variable n directly to your .click () event handler, because n Even though the event is in the handler's realm, jQuery methods are not used in the .data () manner.

No comments:

Post a Comment