Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:22:36+00:00 2026-06-18T05:22:36+00:00

I have user and workspace models with a belongsTo / hasMany relationship: App.User =

  • 0

I have user and workspace models with a belongsTo / hasMany relationship:

App.User = DS.Model.extend({
  name: DS.attr('string'),
  workspace: DS.belongsTo('App.Workspace')
});

App.Workspace = DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('App.User')
});

I have a controller and view setup with the controller’s model property set to a valid user.

App.ApplicationRoute = Ember.Route.extend({
  setupController: function() {
    this.controllerFor('test').set('model', App.User.find(1));
  }
});

The following code and output shows that the belongsTo association has not been loaded at the time the code is executed. What is the proper way to access the workspace (through the user) from the view class?

App.TestView = Ember.View.extend({

  didInsertElement: function() {
    var self = this;

    console.log('first try: ');
    console.log(this.get('controller.model.workspace'));

    setTimeout(function() {
      console.log('second try: ');
      console.log(self.get('controller.model.workspace'));
    }, 1000);
  }
});

Outputs;

// first try: 
// null 
// second try: 
// Class { ... }

I can access the workspace in the template via {{model.workspace}} — how do I do the same thing in the view class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:22:38+00:00Added an answer on June 18, 2026 at 5:22 am

    What is the proper way to access the workspace (through the user) from the view class?

    Your method this.get('controller.model.workspace') is the right way to access the workspace from your view. But as your example shows the didInsertElement() hook is the wrong place to do it. Since App.User.find(1) is asynchronous, you cannot count on model.workspace (or even model itself) being available when the template is being rendered. That’s why your console.log returns null the first time.

    This is perhaps not obvious since Ember works very hard to eliminate explicit forms of asynchronous behavior. Since it returns a model reference right away we might expect App.User.find() to be synchronous but it’s not. Have a look at managing-asynchrony for a closer look at the details.

    I can access the workspace in the template via {{model.workspace}} — how do I do the same thing in the view class?

    Behind the scenes, handlebars is binding {{model.workspace}} to your model and re-rendering the template when the value changes. You should use a similar technique from your view class. Depending on what you are looking to accomplish, you may want to use a binding, computed property, or an observer. For example:

    App.TestView = Ember.View.extend({
    
      workspaceChanged: function() {
        console.log('workspaceChanged to: ');
        console.log(this.get('controller.model.workspace'));        
      }.observes('controller.model.workspace')
    
    });
    

    Also see Bindings. Observers, Computed Properties: What Do I Use When?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Models: Workspace User Asset WorkspaceUser WorkspaceAsset Workspace has many users and
Lets have this String for example: C:\Users\User\workspace\Iterator\src\Iterate_Choose.java C:\Users\User\workspace\Iterator\src\testing.java How can I get the string
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull
In my rails app I have user, school, and course models. I have set
I have user control in my MVC2 app placed in the Content folder (it's
I have User and Teacher models. Teacher belongs_to User and User has_one Teacher. Also
Let's say I have User model and Post model. Post model contains field user_id
I have an app called workspace, somewhere in my form_valid definition in my def
I have a User model, which has a workplace attribute. The Workplace is not
I have a script which is copy the file source to user workspace when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.