1

So on my app, as it is loading in a table from the rails DB side, it comes in very late, and my little $('tr:even').addClass("trAlt"); - only reads 1 tr, and doesn't apply the css class for styling. I have placed the script on didInsertElement as follows:

App.RecordsView = Ember.View.extend({
    templateName: 'records/index',
    contextBinding: 'App.RecordController',
    didInsertElement: function(){
         $('tr:even').addClass("trAlt")
    }
});

But the inital load is 1 tr row (the headings), and then Ember isn't firing off the even as the view changes. Most of the code is the same as from this question.

3 Answers 3

2

Consider using the blockless form of {{each}} and move the jQuery code to didInsertElement of each row as follows:

App.RecordsView = Ember.View.extend({
    templateName: 'records/index',
    contextBinding: 'App.RecordController'
});

App.RecordRowView = Ember.View.extend({
    templateName: 'records/record',
    tagName: 'tr',
    didInsertElement: function(){
      $('tr:even').addClass("trAlt")
    }
});

// in records/index.hbs
{{each controller itemViewClass="App.RecordRowView"}} 

http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_each

Sign up to request clarification or add additional context in comments.

1 Comment

I like this idea, but Don't understand App.MyView, where did that come from? My calling of that doesn't work. I get: Uncaught Error: assertion failed: Unable to find view at path 'App.MyView' ember.js:52
0

Can you use plain CSS to automatically style the table? This will apply to the rows even if they are added after didInsertElement() is called.

CSS:

tr:nth-of-type(even) {
  background-color:red;
}

JSBin example

Comments

0

You can observe the isLoaded property of controller's content:

App.RecordsView = Ember.View.extend({
    templateName: 'records/index',
    contextBinding: 'App.RecordController',

    onRecordsLoaded: function () {
        $('tr:even').addClass("trAlt");
    }.observes('App.RecordController.content.isLoaded')
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.