小编nat*_*ess的帖子

Backbone click事件触发所有集合而不是模型的事件

无法弄清楚出了什么问题.当我点击模型标题时,它会立即获取集合中的所有模型,而不是获取一个模型.如果我将此事件从logView移动到logsView它可以正常工作但无法访问模型,那么我可以使用索引或ant其他模型的ID找到此模型,但不要认为这是一个很好的方法.

var Log = Backbone.Model.extend({});


window.LogsList = Backbone.Collection.extend({
    model:Log,

    url:function (tag) {
        this.url = '/logs/' + tag;
        return this;
    }
});

window.colList = new LogsList();

window.logView = Backbone.View.extend({
    el:$('.accordion'),

    template:_.template($('#log').html()),

    initialize:function () {
        this.model.bind('add', this.render, this);
    },

    events:{
        "click .accordion-toggle" :"getLogBody"
    },

    render:function () {
        return this.template(this.model.toJSON());
    },

    getLogBody:function () {
        this.model.fetch();
    }
});

window.LogsView = Backbone.View.extend({
    el:$("#content"),

    initialize:function (options) {
        colList.bind('reset', this.addAll, this);
        colList.url(options.data).fetch();
    },

    addOne:function (model) {
        var view = new logView({model:model});
        $("#accordion").append(view.render());
    },

    addAll:function () {
        colList.each(this.addOne); …
Run Code Online (Sandbox Code Playgroud)

events backbone.js

7
推荐指数
1
解决办法
3105
查看次数

Rails,将回调包含在无表格模型中的正确方法是什么

我正在扩展并包含这些文件,但仍然收到: undefined method after_initialize for Play:Class

class Play
  extend ActiveModel::Callbacks
  extend ActiveModel::Naming

  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks
  include ActiveModel::Conversion

  after_initialize :process_data
  #...
end
Run Code Online (Sandbox Code Playgroud)

我正在使用Rails 4.

ruby-on-rails

5
推荐指数
2
解决办法
4517
查看次数

在Elixir中附加到文件的最佳方法是什么?

我一直坚持一些简单的任务.我们假设我们有一些伪代码:

Enum.each 1..1_000_000, fn(id) ->
    some_complex_method(id) |> save_results
end
Run Code Online (Sandbox Code Playgroud)

哪里save_results

def save_results(data) do
    {:ok, file} = File.open "data.log", [:append]
    Enum.each(data, &(IO.binwrite(file, &1)))
    File.close file
end
Run Code Online (Sandbox Code Playgroud)

问题是,如果我们的范围非常大,我们会花时间打开和关闭文件.工作完成后如何处理打开状态和调用close方法?

elixir

5
推荐指数
1
解决办法
2686
查看次数

如何在select中使用has_many关系?

给定ProblemObserver型号:

class Problem < ActiveRecord::Base
  has_many :observers
  accepts_nested_attributes_for :observers
end

class Observer < ActiveRecord::Base
  belongs_to :problem
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

我试图用来form_for选择用户作为观察者:

        <%= f.fields_for :observers do |obs| %>
            <%= obs.collection_select(:user_id, Users.to_a, :id, :name, {:include_blank => true, include_hidden: false}, {:multiple => true}) %>
        <% end %> 
Run Code Online (Sandbox Code Playgroud)

但是,Rails会为select:生成错误的名称problem[observers_attributes][0][user_id][],因此即使为strong_params({:observers_attributes => [{:user_id => []}]})创建规则,它也会创建错误的关系,只有problem_id会转到数据库,并且所有user_id都会被忽略.

我要做的是向所有用户显示多个select,抓取ID并在Problem#new方法中为它们创建关联.

更新12.10

发布参数:

参数: {"utf8"=>"?", "authenticity_token"=>"NHDl/hrrFgATQOoz9A3OLbLDAbTMziKMQW9X1y2E8Ek=", "problem"=>{"problem_data_attributes"=>{"title"=>"safasfasfafsasf", "description"=>""}, "observers_attributes"=>{"0"=>{"user_id"=>["5", "8"]}}}}

强大的参数:

def problem_params
 params.require(:problem).permit({:files_attributes => [:attach_id]}, {:observers_attributes => {:user_id => …
Run Code Online (Sandbox Code Playgroud)

has-many ruby-on-rails-4

3
推荐指数
1
解决办法
3747
查看次数