Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

can.batch.start callback called too early in can.Map.extend() init?

$
0
0
I'm creating a plugin that will handle CSS transitions automatically. In certain situations where you would've created a can.List, you would do so with can.TransitionList and any objects listed within (can.Maps) are extended with a @transition attribute (which is a can.TransitionMap).

Here is how it'd work in a can.Component:
  1. init:
  2. {
  3.     this.scope.items.push( {message:"asdf"} );
  4. },
  5. scope:
  6. {
  7.     items: new can.TransitionList()
  8. }
  1. {{#each items}}
  2.     <div data-transition-id="{{@transition.id}}">
  3.         {{message}}
  4.     </div>
  5. {{/each}}
It removes most of the transition stuff from your visible code. This mostly works fine, except that upon inserting the @transition attribute, the DOM code I have in a can.batch.start cannot find the element I'm targeting even though I can see it in dev tools. My only estimation is that can.batch.start's callback is running too soon. Here's the code:
  1. can.TransitionMap = can.Map.extend(
  2. {
  3.     mapCount: 0
  4. },
  5. {
  6.     init: function()
  7.     {
  8.         can.batch.start( this.proxy(function()
  9.         {
  10.             // empty collection, yet it's visible in dev tools elements tree
  11.             console.log( can.$("[data-transition-id="+this.attr("id")+"]") );
  12.         }) );
  13.         
  14.         this.attr("id", this.constructor.mapCount++);
  15.         
  16.         can.batch.stop();
  17.     }
  18. });


  19. can.TransitionList = can.List.extend(
  20. {
  21.     init: function()
  22.     {
  23.         this.bind("add", function(event, newElements, index)
  24.         {
  25.             can.batch.start();
  26.             
  27.             newElements.forEach(function(element, i, list)
  28.             {
  29.                 element.attr( "@transition", new can.TransitionMap() );
  30.             });
  31.             
  32.             can.batch.stop();
  33.         });
  34.     }
  35. });

Viewing all articles
Browse latest Browse all 3491

Trending Articles