繁体   English   中英

与brunch.io的主干。验证集成(主干.js框架)

[英]backbone.validation integration with brunch.io( backbone.js framework)

我如何将http://thedersen.github.com/backbone.validation/与brunch.io集成在一起,我尝试将骨干.validation.js放在供应商文件夹中,

View = require './view'
template = require './templates/home'
User = require 'models/user'

module.exports = class HomeView extends View
 id: 'home-view'
 template: template

initialize: ->

 Backbone.Validation.bind(this)

 @user = new User

 console.log @user

 @user.validate()

给出错误未捕获的TypeError:对象#没有方法'validate'

然后我也尝试了

# The application bootstrapper.
Application =
 initialize: ->
HomeView = require 'views/home_view'
Router = require 'lib/router'
User  = require 'models/user'
# Ideally, initialized classes should be kept in controllers & mediator.
# If you're making big webapp, here's more sophisticated skeleton
# https://github.com/paulmillr/brunch-with-chaplin
@homeView = new HomeView model: new User

# Instantiate the router
@router = new Router()
# Freeze the object
Object.freeze? this

module.exports = Application

这也

Model = require './model'
HomeView = require 'views/home_view'


 module.exports = class User extends Model

 defaults:
    logged_in: false,
    token: false

 initialize: ->
   new HomeView model: new User

 validation: 
     email: {
       required: true,
       pattern: 'email',
       msg: 'Please enter a valid email' },
     name:  {
       required: true,
       msg: "Name is required" }   

给出错误未捕获的RangeError:超出最大调用堆栈大小

因此,没有一个可行的技巧可以帮助我解决这个问题。

您应该在vendor / scripts文件夹中放入了ribs.validation.js。 您是否看到必须在插件之前先取消Backbone和Underscore?

早午餐使用配置文件,您应该在config.coffee中添加以下内容:

exports.config =
  # See docs at http://brunch.readthedocs.org/en/latest/config.html.

  # Edit the next line to change default build path.
  paths:
    public: 'public'

  files:
    javascripts:
      # Defines what file will be generated with `brunch generate`.
      defaultExtension: 'coffee'
      # Describes how files will be compiled & joined together.
      # Available formats:
      # * 'outputFilePath'
      # * map of ('outputFilePath': /regExp that matches input path/)
      # * map of ('outputFilePath': function that takes input path)
      joinTo:
        'javascripts/vendor.js': /^vendor/
        'javascripts/app.js': /^app/
      # Defines compilation order.
      # `vendor` files will be compiled before other ones
      # even if they are not present here.
      order:
        before: [
          'vendor/scripts/console-helper.js',
          'vendor/scripts/jquery-1.7.2.js',
          'vendor/scripts/underscore-1.3.3.js',
          'vendor/scripts/backbone-0.9.2.js'
          'vendor/scripts/backbone.validation.js'
        ]

重要的是订单部分。

1)您将验证事物绑定到视图,而不是用户,所以这就是为什么您遇到第一个错误的原因

2)您不应该在模型内部创建视图,

暂无
暂无

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

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