Friday, December 19, 2014

Sending data to CanJS component

Ideally, components should be independent, but sometimes it is needed to send data or call function on component from outside. It is possible to do by using observable like can.Map and listen to events on it from component like:

<script type="text/javascript">
  var observable = new can.Map({val:0});

  $(function() {
    can.Component.extend({
      tag: "my-tag",
      events: {
        "{observable} val": function(param, param2, newval){
          alert(newval);
        }
      }
    });
    $('#myTagDiv').html(can.view.mustache('<my-tag/>'));
  });
</script>



<div id="myTagDiv"></div>
<input type="button" value="Call observable" onclick="observable.attr('val', observable.val+1);">


This way all components will receive observable change event either they are sent from itself, other component or directly from JavaScript.

1 comment:

  1. Now, you know all of 코인카지노 the fundamentals related to Internet casinos, so feel free choose on} a dependable gaming establishment and start your journey to the thrilling world of online playing. “We're not going to get that cash again,” mentioned Brown, who believes state and federal legislation enable for tribes to host web playing. North Dakotans poured virtually $1.seventy five billion into the machines in fiscal 2022. The company is alleged to be soliciting traders online to buy Slotie NFTs that purportedly provide the traders with ownership pursuits in online and metaverse casinos and the right to share passively in the revenue of the casinos.

    ReplyDelete