Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ember.js中,我在哪里计算样式字符串?_Javascript_Ember.js - Fatal编程技术网

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

Javascript 在ember.js中,我在哪里计算样式字符串?,javascript,ember.js,Javascript,Ember.js,我有一个ember.js应用程序 var App = Ember.Application.create({ LOG_TRANSITIONS: true }); App.ApplicationAdapter = DS.FixtureAdapter; //==============================ROUTER============================== App.Router.map(function() { this.resource('organisa

我有一个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'});
  });
});
我无法计算出如何从复杂的夹具数据中精确地计算材料。我的模型包括一个有建筑物和建筑群的组织

//==============================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')
});
我需要根据整个建筑群计算东西,就像在value_range函数中一样。这似乎很有效

我有这些固定装置

//==============================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},
];
我已经设法为一个组织创建了一个路由和一个索引路由,它应该显示默认的(“主”)组

我想展示一个简单的条形图,其中包含组中每个建筑的计算值(左/右和宽度值)。计算使用
组.value\u范围
数据以及
建筑.value
数据。我的问题是我不知道该把这个函数放在哪里。控制员似乎无法进入各个建筑物

我是否使用车把助手?我把它拼凑在一起了,但它有味道

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>'
  );
});
Ember.handlebar.helper('thing',function(building,group){
var range=group.get('value_range');
var value=building.get('value');
变量宽度=数学绝对值(值)/(范围最大值-范围最小值)*100;
var zero_position=数学绝对值(最小范围)/(最大范围-最小范围)*100;
如果(值>=0){
左或右=‘左’;
myclass='pos';
}否则{
左或右='右';
myclass='neg';
零位=100-零位;
}
返回新把手。安全字符串(
'-'
);
});
还是我需要一个视野?文档称视图主要用于事件处理


在这件事上,我并不是在摸索余烬之路。有人能帮忙吗?

这里有一个更惯用的方法。我只是翻译了你的例子,我没有测试这个。但它应该给你足够的提纲,让它发挥作用:

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')

});
从模板中使用此选项,如:

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