从Javascript对象Ember获取属性

时间:2015-01-18 10:36:34

标签: javascript ruby-on-rails ember.js ember-data

我有一个Rails和Ember应用程序,有两个模型Image和Project。图像属于项目,项目有很多图像。

我正在尝试从给定项目中返回第一个图像以显示在我的项目索引页面上,但似乎无法找到图像对象。

目前我有以下内容:

app/assets/javascripts/templates/projects.js.emblem

section#portfolio
  .container
    .row
      .col-xs-8.col-xs-offset-2
        .row
          each project in controller
            .col-xs-12.col-sm-6
              p = project.firstImage
              link-to 'project' project
                h4.text-center.text-uppercase
                  = project.client
              h5.text-center
                em
                  = project.tags 


app/assets/javascripts/models/project.js

App.Project = DS.Model.extend({
  name: DS.attr('string'),
  client: DS.attr('string'),
  tags: DS.attr('string'),
  description: DS.attr('string'),
  start_date: DS.attr('string'),
  end_date: DS.attr('string'),

  images: DS.hasMany('image'),

  projectDuration: function() {
    return this.get('start_date') + ' - ' + this.get('end_date')
  }.property('start_date', 'end_date'),

  firstImage: function() {
    var projectImages = this.get('images');
    var projectImage = projectImages.get('firstObject');
    return projectImage;
  }.property('images')

});

图像具有id,project_image(URL的字符串)和project_id的属性。

当前设置会在视图中显示<App.Image:ember323:6>哪个是正确的对象,但我似乎无法从对象中获取project_image字符串?

非常感谢任何帮助:)


抱歉忘了分享我的图片模型

app/assets/javascripts/models/image.js
App.Image = DS.Model.extend({
  _project_image: DS.attr('string'),
  _project_id: DS.attr('number'),


  project: DS.belongsTo('project')
});

0 个答案:

没有答案