繁体   English   中英

在带有requirejs的主干中使用jquery插件

[英]using jquery plugin in backbone with requirejs

我正在尝试在带有requirejs的主干应用程序中使用此插件: https : //github.com/mattbryson/TouchSwipe-Jquery-Plugin ,我想我一定不太了解requirej和主干网如何交互。 我已经阅读了很多东西,所以我想确保可以使用我已经建立的项目结构来完成这项工作,我认为这是相当普遍的。 这是我的应用程序:

我加载requirejs并传入主js文件:

<script src="/assets/js/bower_components/requirejs/require.js" data-main="/assets/js/app/main"></script>

在main.js中,我加载了所有主干路径:

    require.config({
      paths: {
        jquery: '../bower_components/jquery/dist/jquery.min',
        underscore: '../bower_components/underscore/underscore',
        backbone: '../bower_components/backbone/backbone',
        touchswipe: '../bower_components/jquery-touchswipe/jquery.touchSwipe.min',
        templates: 'templates'
      }
    });

    require([
      'app',
    ], function(App){
      App.initialize();
    });

In app.js, I just initialize my router, and in router.js, I initialize my views:

    app_router.on('route:defaultAction', function (actions) {
            var homeView = new HomeView();
            homeView.render();
        });

In my view, I need to be able to figure out how to use the plugin. I can tell the source has loaded, but I can't call events as documented. 

这是一个示例视图:

define([
  'jquery',
  'underscore',
  'backbone',
  'text!templates/products/productsItemsTemplate.html',
  'collections/products/ProductsItemsCollection',
  'imagesloaded',
  'touchswipe'

], function($, _, Backbone,productsItemsTemplate,ProductsItemsCollection,imagesloaded,touchswipe){
imageLoader: imagesLoaded, //this plugin worked by defining it again here, but this feels like the wrong way to do things, and also, it didn't rely on a dom object in order to work, which i feel might be a problem with touchSwipe

touchSwipe: touchswipe,

el: $(".products-items-container"),

events : {

},

initialize: function(){
  $(".product-items-container").swipe( { //doesn't seem to work, but doesn't throw errors either
    swipe:function(event, direction, distance, duration, fingerCount, fingerData) {
      console.log(direction);
    },
     threshold:0
  });

},

请注意我对imagesloaded插件的评论,该插件可以正常工作(尽管我认为是通过一种怪异的方式)。

有人可以清理吗? 在标准javascript中使用这些插件似乎非常容易,但是骨干真的让我失望。

看起来一切正常:

http://jsfiddle.net/nitincool4urchat/zzhpmhcd/1/

require.config({
    paths: {
        "jquery": "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min",
            "underscore": "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min",
            "backbone": "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min",
            "text": "https://rawgit.com/requirejs/text/master/text"
    }
});

require(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
    var view = Backbone.View.extend({
        initialize: function () {
            require(["//cdnjs.cloudflare.com/ajax/libs/jquery.touchswipe/1.6.4/jquery.touchSwipe.min.js"], function () {
                $("#test").swipe({
                    //Generic swipe handler for all directions
                    swipe: function (event, direction, distance, duration, fingerCount, fingerData) {
                        $(this).text("You swiped " + direction);
                    },
                    //Default is 75px, set to 0 for demo so any distance triggers swipe
                    threshold: 0
                });
            });
        }
    });
    new view();
});

我更喜欢仅在上述代码中要求的时候才调用插件。 即使我们提早加载它们,它也能正常工作。

http://jsfiddle.net/nitincool4urchat/zzhpmhcd/2/

require.config({
    paths: {
        "jquery": "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min",
            "underscore": "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min",
            "backbone": "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min",
            "text": "https://rawgit.com/requirejs/text/master/text",
            "swipe": "//cdnjs.cloudflare.com/ajax/libs/jquery.touchswipe/1.6.4/jquery.touchSwipe.min"
    }
});

require(["jquery", "underscore", "backbone", "swipe"], function ($, _, Backbone) {
    var view = Backbone.View.extend({
        initialize: function () {
            $("#test").swipe({
                //Generic swipe handler for all directions
                swipe: function (event, direction, distance, duration, fingerCount, fingerData) {
                    $(this).text("You swiped " + direction);
                },
                //Default is 75px, set to 0 for demo so any distance triggers swipe
                threshold: 0
            });
        }
    });
    new view();
});

暂无
暂无

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

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