Attaching component to a node that is dynamically added.

71 views
Skip to first unread message

appren...@googlemail.com

unread,
Oct 18, 2014, 6:50:41 PM10/18/14
to twitter...@googlegroups.com
I have a Component where I'm using jquery to append an element to the document (inside body element). I'd like reuse one of the components that I have already created and attach it to the newly added element. Not sure how I can do this?

Example

Given the Foo component, I'd like to attach the component to the "foobar" element (see below) upon append.

define(function(require) {
  'use strict';
  var Foo = require('component/foo');
  ...

  function initialize() {
    ...
    Foo.attachTo('body');
  }

  return initialize;

});

//Attach the Foo Component to the "foobar" element below.

 define(function(require) {
     var defineComponent = require('flight/lib/component');
     return defineComponent(data);
     function boo() {
        this.boo = function(){
           var elem = "div id="foobar"></div>"
            this.$node.append(elem);
            ....
         
        }

    this.after('initialize', function(){      
         .....

     });
   
     }
     });

Gabriel Pugliese

unread,
Oct 18, 2014, 9:56:34 PM10/18/14
to appren...@googlemail.com, twitter...@googlegroups.com
Is that a real code? If it is, correct the creation of the fragment:

var elem = $("<div>").attr('id', 'foobar');



Gabriel Pugliese
CodersTV.com
@coderstv
Message has been deleted

Giuseppe Gurgone

unread,
Oct 20, 2014, 7:45:45 AM10/20/14
to twitter...@googlegroups.com
Refactor your component to use event delegation instead https://github.com/flightjs/flight/blob/master/doc/base_api.md#this.on (3rd example).
You can then attach the component to the closest parent - in your case boo's this.$node.
Reply all
Reply to author
Forward
0 new messages