1

我的代码:

var Backbone = require('backbone');
var $ = require('jquery');
Backbone.$ = $;
var velocity = require('velocity-animate');

module.exports = function (obj) {

    var obj = obj || {};

    return Backbone.View.extend({
        tagName: 'article',
        className: 'lab-notes__article--single',
        initialize: function () {
            var _this = this;
            _this.collection.deferred.done(function () {
                var _thePost = _this.collection.first();
                _this.model = _this.collection.find(function(model) {
                    return model.get('slug') === data.slug;
                });
                _this.render();
            });
        },
        render: function () {
            var _this = this;
            var data = this.model.attributes;
            var JST = require('../templates/blog-post-single.html')(data);
            this.$el.html(JST);
            $('[data-blog-post-single]').html(this.el);

            var openBlogPost = [
                { e: $('body'), p: 'scroll', o: { duration: 100, duration: 500, easing: 'easeInOutQuart', } },
                { e: _this.$el, p: 'fadeIn', o: { duration: 200 } }
            ];
            $.velocity.RunSequence(openBlogPost);
        }
    });
};

…以及我在运行它时收到的错误:

Uncaught TypeError: Cannot read property 'RunSequence' of undefined

这发生在这一行:

$.velocity.RunSequence(openBlogPost);

我不确定发生了什么,但我觉得这与我对 Velocity 的需求有关。有什么建议么?

4

2 回答 2

1

为了让它工作,我不得不从这个改变 require 行:

var velocity = require('velocity-animate');

……对此:

velocity = $.velocity = require('velocity-animate');
于 2015-05-19T14:31:09.197 回答
0

文档包含有关使用 browserify 的部分:

window.jQuery = window.$ = require("path/to/jquery-x.x.x.js");
require("path/to/velocity.js");
// Optional: If you're using the UI pack, require it after Velocity. (You don't need to assign it to a variable.)
require("path/to/velocity.ui.js");

不要创建一个名为的局部变量,而是$尝试在全局(aka window)范围内创建它以及jQuery命名空间。另外,确保velocity-animate指向正确的文件(我记得它是 npm 上的包的名称,而不一定是项目中 Velocity 的路径)。

于 2015-05-19T06:56:01.787 回答