简体   繁体   English

我在ember.js中的哪里计算样式字符串?

[英]Where do I calculate my style strings in ember.js?

I have an ember.js app 我有一个ember.js应用

var App = Ember.Application.create({
  LOG_TRANSITIONS: true
});
App.ApplicationAdapter = DS.FixtureAdapter;

//==============================ROUTER==============================

App.Router.map(function() {
  this.resource('organisation', {path: '/:org_slug/:org_id'}, function () {
    this.resource('building', {path: '/:hall_slug/:hall_id'});
    this.resource('group', {path: '/:grp_slug/:grp_id'});
  });
});

I cannot work out how to neatly calculate stuff from my complex fixtures data. 我无法弄清楚如何从复杂的灯具数据中巧妙地计算出东西。 My models include an organisation with buildings and building groups. 我的模型包括一个包含建筑物和建筑物组的组织。

//==============================MODELS==============================

App.Organisation = DS.Model.extend({
  name: DS.attr('string'),
  buildings: DS.hasMany('building', {async: true}),
  groups: DS.hasMany('group', {async: true})
});

App.Building = DS.Model.extend({
  organisation: DS.belongsTo('organisation'),
  name: DS.attr('string'),
  value: DS.attr('number'),
  groups: DS.hasMany('group', {async: true})
});

App.Group = DS.Model.extend({
  organisation: DS.belongsTo('organisation'),
  buildings: DS.hasMany('building', {async: true}),
  name: DS.attr('string'),
  start: DS.attr('date'),
  end: DS.attr('date'),
  main: DS.attr('boolean'),
  value_range: function() {
    var maximum = 0.0;
    var minimum = 0.0;
    this.get('buildings').forEach(function(building) {
      var v = building.get('value');
      maximum = Math.max(maximum, v);
      minimum = Math.min(minimum, v);
    });
    return {'maximum': maximum, 'minimum': minimum};
  }.property('buildings.@each.value')
});

I need to calculate stuff based on the whole building group as in the value_range function. 我需要像value_range函数中那样基于整个建筑组来计算东西。 This seems to work fine. 这似乎很好。

I have these fixtures 我有这些装置

//==============================FIXTURES==============================

App.Organisation.FIXTURES = [
  { id: 1, name: 'Organisation 1', buildings: [1,2,3,4,5,6,7,8,9,10], groups: [1, 2, 4]},
  { id: 2, name: 'Organisation 2', buildings: [11,12,13,14,15,16], groups: [3]}
];


App.Building.FIXTURES = [
  { id: 1, name: 'Building 1', value: -3.2, groups: [1], organisation_id: 1},
  { id: 2, name: 'Building 2', value: 23.2, groups: [1,2], organisation_id: 1},
  { id: 3, name: 'Building 3', value: 34.2, groups: [1,2], organisation_id: 1},
  { id: 4, name: 'Building 4', value: -3.12, groups: [2], organisation_id: 1},
  { id: 5, name: 'Building 5', value: 0.12, groups: [3], organisation_id: 2},
  { id: 6, name: 'Building 6', value: 0.2, groups: [3], organisation_id: 2}
];


App.Group.FIXTURES = [
  {id: 1, organisation_id: 1, name: 'Group 1', buildings: [1,2,3], main: true},
  {id: 2, organisation_id: 1, name: 'Group 2', buildings: [2,3,4], main: false},
  {id: 3, organisation_id: 2, name: 'Group 3', buildings: [5,6], main: true},
];

And I have managed to create a route for an organisation and an index route which should show the default ('main') group. 而且我设法为组织创建了一个路由,并为索引路由创建了显示默认(“主”)组的索引。

//==============================ROUTES==============================
App.OrganisationRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('organisation', params.org_id);
  },
  serialize: function(model) {//add prefix to slug and id
    return {
      org_slug: model.get('slug'),
      org_id: model.get('id')
    };
  }
});
App.OrganisationIndexRoute = Ember.Route.extend({
  model: function(params) {
    //get buildings from the main group
    return this.modelFor('organisation').get('groups').then(function(grps) {
      return grps.findBy('main', true).get('buildings');
    });
  },
  setupController: function(controller, model) {
    this._super(controller, model);
    controller.set('organisation', this.modelFor('organisation'));
    var mygroup = this.modelFor('organisation').get('groups').then(function(grps) {
      controller.set('group', grps.findBy('main', true));    
    });
  }
});

I want to present a simple bar styled with calculated values for each building in the group (left/right and width values). 我想展示一个简单的栏,其样式为组中每个建筑物的计算值(左/右和宽度值)。 The calculation uses the group.value_range data as well as the building.value data. 该计算使用group.value_range数据以及building.value数据。 My problem is that I can't work out where to put this function. 我的问题是我无法弄清楚该函数的位置。 The controller doesn't seem to have access to individual buildings. 控制器似乎无法访问各个建筑物。

Do I use a handlebars helper? 我是否使用车把帮手? I have hacked this together but it smells. 我已经一起破解了,但是闻起来了。

Ember.Handlebars.helper('thing', function(building, group) {
  var range = group.get('value_range');
  var value = building.get('value');
  var width = Math.abs(value)/(range.maximum - range.minimum) * 100;
  var zero_position = Math.abs(range.minimum)/(range.maximum - range.minimum) * 100;
  if (value >= 0) {
    left_or_right = 'left';
    myclass = 'pos';
  } else {
    left_or_right = 'right';
    myclass = 'neg';
    zero_position = 100 - zero_position;
  }
  return new Handlebars.SafeString(
    '<div class="my-bar ' + myclass + '" style="width: ' + width + '%; ' + left_or_right + ': ' + zero_position + '%;">-</div>'
  );
});

Or do I need a view? 还是我需要一个视图? The docs say views are mainly for event processing. 文档说视图主要用于事件处理。

I'm not quite groking the ember way on this. 我在这个问题上还不太了解。 Can anyone help? 有人可以帮忙吗?

Here's a more idiomatic way to do it. 这是一种更惯用的方法。 I just translated your examples, I didn't test this. 我只是翻译了您的示例,没有对此进行测试。 But it should give you enough of an outline to make it work: 但是它应该给您足够的轮廓以使其起作用:

App.RangeBar = Ember.Component.extend({
  classNames: ['my-bar'],
  classNameBindings: ['direction'],
  attributeBindings: ['style'],

  direction: function(){
    if (this.get('value') >= 0) {
      return 'pos';
    } else {
      return 'neg';
    }
  }.property('value'),

  width: function(){
    return Math.abs(this.get('value'))/(this.get('max') - this.get('min')) * 100;
  }.property('value', 'max', 'min'),

  zeroPosition: function(){
    return Math.abs(this.get('min'))/(this.get('max') - this.get('min')) * 100;
  }.property('min', 'max'),

  style: function(){
    var styles = [
      ['width', this.get('width') + '%']
    ];
    if (this.get('direction') === 'pos') {
      styles.push(['left', this.get('zeroPosition') + '%']);
    } else {
      styles.push(['right', (100 - this.get('zeroPosition')) + '%']);
    }
    return styles.map(function(style){return style[0] + ":" + style[1];}).join(";");
  }.property('width', 'zeroPosition', 'direction')

});

Use this from your template like: 从您的模板中使用它,例如:

{{range-bar max=group.value_range.maximum min=group.value_range.minimum value=building.value}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM