jQuery: FadeIn/To and SlideDown new element
Asked Answered
H

4

6

Here is the one line of code I use to push a new row to my container:

this.$el.append(new ItemView(item).render().el);

Where item is a Backbone.js model, render() creates and/or modifies the object and el is the html element. (The object is never displayed until rendering is complete)

How can I keep** new ItemView(item).render() **and store it in a variable, then fade and slide it into (the bottom) of my container?

Edit

Please keep in mind that this.$el is the container element.

Hayton answered 14/2, 2012 at 16:0 Comment(0)
S
14
var rendered = new ItemView(item).render();

$(rendered.el).appendTo(this.$el).hide().fadeIn().slideDown();
Seanseana answered 14/2, 2012 at 16:2 Comment(1)
I had to do something different, but thanks for showing me the way, @Linus.Dobrinsky
L
1

Set attribute to your element style="display: none;" so when you append it it wont be visible. Then after add exec fadein function and your element will be visible.

I hopt this helps.

Lungwort answered 14/2, 2012 at 16:3 Comment(1)
Got an example of how to do this?Hayton
G
0

In my opinion the problem is with having the view's render method return the view.

Your view probably looks something like this:

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html);   

          return this;          

       }  
    });

Then somewhere you have a method that creates the view and attaches it to the DOM.

 var $main = $('#main'); //main area in the DOM eg: <div id="main"></div>
 $main.append(new Somveview({model: item}).render().$el);

The problem is that render() must be called to gain access to the view's element... Think about this, when a change is made on the model the view is going to render again. This means if you want to add any transition effects such as fading or sliding your likely going to add them in the view's render method.

Add some effects into the views render method.

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html).hide().slideDown(600);   

          return this;          

       }  
    });

This works, any change to the model will cause the view to render and we will see the slide down effect. But only because the view has already been appended to the DOM!!! THE PROBLEM, we notice that the slideDown effect does not happen on page load. So, as a patch, we duplicate the effect in the place where we are creating the view and appending it to the DOM.

The reason why the effect does not occur on page load is because the effect has already happened before we are appending the view's element to the DOM.

But why duplicate the effect, it makes sense to me to change the view a little.

var Someview = Backbone.View.extend({

       initialize: function () {

          this.template = _.template($("#someview-template"));  

          this.model.on('change', this.render());

       },
       render: function() { 

          var html = this.template(this.model);    

          this.$el.html(html);   

          //no longer returning the views object from the render method. 

       }  
    });

Create the view and append it to the DOM

 var $main = $('#main'); //main area in the DOM eg: <div id="main"></div>
 var someview = new Somveview({model: item}); //create the view
 $main.append(someview.$el); // add the view's element to the DOM
 someview.render(); // Rendering after we have appended to the DOM !!
Gymnasiast answered 27/7, 2013 at 22:8 Comment(0)
D
0

This also appears to work

this.$el.append($(new ItemView(item).render().el).hide().fadeIn());
Dolli answered 6/3, 2015 at 22:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.