Ruby on Rails-如何通过js获取特定的对象属性?

时间:2019-02-13 12:03:32

标签: ruby-on-rails coffeescript

我一直坚持在Rails中获取特定的对象属性。 如果执行console.log(installable),我可以看到所有对象属性,但是由于某些原因,我只能使用其中一些。

我需要从属性中获取制造商名称。我尝试了几种不同的方法,但它们似乎都不起作用。结果总是“未定义”。但是,如果我尝试从属性获取ID,那么它就可以工作。我可以在控制台日志属性部分中看到ID和制造商名称,但是为什么不能在函数中获得制造商名称?

这一切都在model_view.coffee文件中完成

也:

installable.get('manufacturer_name')-返回未定义

installable.get('id')-返回正确的ID

我尝试了installable.attributes['manufacturer_name']无效,但是installable.attributes['id']有效。

此代码遍历所有几何并找到它们的可安装项:

@geometries.forEach (model) =>

  installable = @findInstallable(model)
  console.log(installable)

找到可安装对象的功能

  findInstallable: (geometry) ->
    installableId = geometry.get('installable_id')
    installable = null

    _.any UPB.InstallationBuilder.installableCollectionRegistry, (collection) =>
      installable = collection.get(installableId)
      !!installable # stop searching after the first match

    installable

这是我从控制台日志中得到的:

Installable {cid: "c468", attributes: {…}, _changing: false, _previousAttributes: {…}, changed: {…}, …}
attributes:
adjusted_type_id: 40
area_m2: 7.89
can_install: false
can_uninstall: false
color_class: "new"
detail_order_deadline: null
drawing_files: []
element_id: 33
id: 330
identical_id: 33
installation_date: null
installation_planning_finished: false
installation_team_id: null
manufacturer_color_code: "#FF6600"
manufacturer_installation_buffer_days: null
manufacturer_name: "ABC Prefab"
manufacturer_type: "Company"
name: "MBD-10032"
part_dimensions: "200 / 6595 / 1200"
project_name: "ABC1 : Concrete (ABC Prefab)"
revision: null
ship_delivery_note_id: null
ship_delivery_note_number: null
source_id: 33
source_type: "Element"
stage_id: 2
state: "new"
transport_by_ship: false
type_color: "#993300"
type_id: 4
url: "/company_stages/2/elements/33"
volume_m3: 0.8477
weight_kg: 2060

0 个答案:

没有答案