Search code examples
requirejsdurandaldurandal-2.0

How call a function when view is attached in Durandal?


I'm just started to develeop a web app with Durandal. I don't understand how call a function from a viewmodel and why if I find an element of my document it seems is not attached yet.

Example: viewmodel.js

    define( ['libone', 'libtwo'], function () {


      $('.carousel').libone({
          expandbuttons: true,
          keyboard: true,
          mouse: true
      });

    });

It doesn't find the ID call carousel is why there's no view.hmtl content but index.html content.

Any ideas? Thanks in advance

UPDATE No errors but the view content is not returned. view.html

<section> 
  <h2 data-bind="html:name"></h2> 
  <blockquote data-bind="html:descr"></blockquote> 
    <div class="carousel"> 
      <div class="carousel-sections"> 
        <div class="carousel-section"> ... some content ... </div> 
      </div> 
    </div> 
  <a id="carousel-scroll-prev" href="#"></a> 
  <a id="carousel-scroll-next" href="#"></a> 
<section> 

modelview.js

define( ['libone', 'libtwo'], function (libone, libtwo) {
  var viewattached = function(view){
    var view = $(view);
    view.find('.carousel').libone({
      expandbuttons: true,
      keyboard: true,
      mouse: true
    });
  };

  var vm = {
    attached: viewattached,
    name: 'How about we start?',
    descr: 'You have many choices to make and many roads to cross...'
  };

  return vm;
});

Only name, descr and scroll are shown but not carousel-section.


Solution

  • The rendering problem has been resolved using compositionComplete instead attached.