小编Nei*_*eil的帖子

Array.apply实际上在做什么

在阅读了这个SO问题后,我仍然对Array.apply实际上在做什么感到困惑.请考虑以下代码段:

new Array(5).map(function(){
  return new Array(5);
});
Run Code Online (Sandbox Code Playgroud)

我希望这可以初始化一个包含5个未定义条目的数组,然后映射它们创建一个5x5的二维数组);

相反,我只是得到数组,好像它从未映射过:

[undefined, undefined, undefined, undefined, undefined]
Run Code Online (Sandbox Code Playgroud)

当我在Array.apply调用中将构造函数调用包装到数组时,然后映射它,它按预期工作:

Array.apply(null, new Array(5)).map(function(){
  return new Array(5);
});
Run Code Online (Sandbox Code Playgroud)

导致;

[[undefined, undefined, undefined, undefined, undefined],
 [undefined, undefined, undefined, undefined, undefined],
 [undefined, undefined, undefined, undefined, undefined],
 [undefined, undefined, undefined, undefined, undefined],
 [undefined, undefined, undefined, undefined, undefined]];
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?Array.apply是另一种调用new Array()或Array.prototype.constructor的方法吗?还有其他情况会有利吗?另外,为什么我的第一个方法没有在我发送的地图上接收?

谢谢!-Neil

javascript arrays constructor

33
推荐指数
2
解决办法
1万
查看次数

has_many:通过NameError:未初始化的常量

我只想制作一个小连接表,最终在该连接上存储额外的信息(这就是为什么我不使用HABTM).从关联的rails文档中我创建了以下模型:

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physicians
  belongs_to :patients
end
Run Code Online (Sandbox Code Playgroud)

我的架构看起来像这样:

ActiveRecord::Schema.define(:version => 20130115211859) do

  create_table "appointments", :force => true do |t|
    t.datetime "date"
    t.datetime "created_at",   :null => false
    t.datetime "updated_at",   :null => false
    t.integer  "patient_id"
    t.integer  "physician_id"
  end

  create_table "patients", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null …
Run Code Online (Sandbox Code Playgroud)

console has-many-through ruby-on-rails-3

9
推荐指数
1
解决办法
2696
查看次数

Backbone.Marionette CollectionView模板'undefined'

我一直在使用Brian Mann的BackboneRails截屏视频,所以我的应用程序结构完全符合这些.

以下定义了HomepageApp Marionette Appication的"show"部分.

My.module('HomepageApp.Show', function(Show, App, Backbone, Marionette, $, _){

  Show.Photo = Marionette.ItemView.extend({
    tagName:'span'
  });

  Show.Photos = Marionette.CollectionView.extend({  
    template: 'homepage/show/templates/photos',

    itemView:Show.Photo,
    itemViewContainer: '#photos'
  });

  Show.Layout = Marionette.Layout.extend({
    template: 'homepage/show/templates/layout',
    regions:{
      photoRegion: '#photo-region'
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

我也用以下内容覆盖Marionette.Renderer.render函数:

Backbone.Marionette.Renderer.render = function(template, data){
  var path = JST["backbone/apps/" + template];  

  try{
    if(!path) throw({message: "Template '" + template + "' not found!"}); 
    return path(data);
  }

  catch(err){
    console.log(err.message);
  }
}
Run Code Online (Sandbox Code Playgroud)

我的所有观点,包括许多未在此处显示的观点都完美无缺.问题是Show.Photos CollectionView的'模板'属性在我的渲染器覆盖中变为'undefined',给出了以下错误:

Template 'undefined' not found!
Run Code Online (Sandbox Code Playgroud)

奇怪的是,当我根本没有为模板属性传递任何值时,甚至会发生这种情况.

我也知道我在实例化时传递了一个有效的Backbone集合.

我完全陷入了困境.有谁熟悉这种现象?

collectionview backbone.js backbone-rails marionette

2
推荐指数
1
解决办法
3694
查看次数