【问题标题】:Jquery not working with meteorjsJquery 不使用meteorjs
【发布时间】:2016-10-08 22:30:29
【问题描述】:

我正在浏览“Meteor in Action”book(myfridge 应用程序)的第一个示例。 在我们为产品添加拖放功能的最后一步中,我的应用似乎无法识别 jquery。我选择了最新版本,因为这本书有点过时了。

我已经在/client/main.html 文件中包含了 jquery CDN 的要求

<head>
  <title>myfridge</title>
  <script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
</head>

在我的/client/main.js 中,我包含了使fridgeproductList 可拖放和productListItem 可拖动的必要声明。

Template.fridge.onRendered( function () {
    var templateInstance = this;

    templateInstance.$('#fridge').droppable({
        drop: function(evt, ui) {
            var query = { _id: ui.draggable.data('id') };
            var changes = { $set: { place: 'fridge' } };
            Products.update(query, changes);
        }
    });
});


Template.productList.onRendered( function() {
    var templateInstance = this;

    templateInstance.$('#supermarket').droppable({
        drop: function(evt, ui) {
            var query = { _id: ui.draggable.data('id') };
            var changes = { $set: { place: 'supermarket'} };
            Products.update(query, changes);
        }
    });
});

Template.productListItem.onRendered( function() {
    var templateInstance = this;

    templateInstance.$('.draggable').draggable({
        cursor: 'move',
        helper: 'clone'
    });
});

我查看了其他有类似问题的帖子,但这些提示似乎对我不起作用。

编辑:控制台中的错误消息是

Exception from Tracker afterFlush function:
debug.js:41 TypeError: templateInstance.$(...).droppable is not a function
    at .<anonymous> (main.js:9)
    at template.js:119
    at Function.Template._withTemplateInstanceFunc (template.js:465)
    at fireCallbacks (template.js:115)
    at .<anonymous> (template.js:208)
    at view.js:107
    at Object.Blaze._withCurrentView (view.js:538)
    at view.js:106
    at Object.Tracker._runFlush (tracker.js:511)
    at onGlobalMessage (setimmediate.js:102)
debug.js:41 Exception from Tracker afterFlush function:
debug.js:41 TypeError: templateInstance.$(...).droppable is not a function
    at .<anonymous> (main.js:22)
    at template.js:119
    at Function.Template._withTemplateInstanceFunc (template.js:465)
    at fireCallbacks (template.js:115)
    at .<anonymous> (template.js:208)
    at view.js:107
    at Object.Blaze._withCurrentView (view.js:538)
    at view.js:106
    at Object.Tracker._runFlush (tracker.js:511)
    at onGlobalMessage (setimmediate.js:102)
debug.js:41 Exception from Tracker afterFlush function:
debug.js:41 TypeError: templateInstance.$(...).draggable is not a function
    at .<anonymous> (main.js:34)
    at template.js:119
    at Function.Template._withTemplateInstanceFunc (template.js:465)
    at fireCallbacks (template.js:115)
    at .<anonymous> (template.js:208)
    at view.js:107
    at Object.Blaze._withCurrentView (view.js:538)
    at view.js:106
    at Object.Tracker._runFlush (tracker.js:511)
    at onGlobalMessage (setimmediate.js:102)
debug.js:41 Exception from Tracker afterFlush function:
debug.js:41 TypeError: templateInstance.$(...).draggable is not a function
    at .<anonymous> (main.js:34)
    at template.js:119
    at Function.Template._withTemplateInstanceFunc (template.js:465)
    at fireCallbacks (template.js:115)
    at .<anonymous> (template.js:208)
    at view.js:107
    at Object.Blaze._withCurrentView (view.js:538)
    at view.js:106
    at Object.Tracker._runFlush (tracker.js:511)
    at onGlobalMessage (setimmediate.js:102)

【问题讨论】:

  • 嗨,你的错误信息是什么?

标签: javascript jquery meteor


【解决方案1】:

您的错误消息是 templateInstance.$(...).droppable is not a function,这意味着您的 client/main.js 无法访问 jQuery UI 可访问的 .droppable() 方法。

如果您只需要 jQuery UI 可拖放功能,您可以安装 drewproud:jquery-ui-droppablemeteor add drewproud:jquery-ui-droppable

【讨论】:

  • 这似乎解决了这个问题。虽然现在当我将一个项目拖放到“冰箱”中时,控制台会弹出这个包装器drewproud_jquery-ui-droppable.js:1725 Uncaught TypeError: Cannot read property 'options' of undefined 的一些内部错误。尽管如此,代码还是按预期工作。
【解决方案2】:

Jquery 作为一个流星包提供

像这样包含它:

meteor add jquery

【讨论】:

  • 我包含了它(并从 html 文件中删除了
  • 问题在于 jQuery UI 不可用,而不是 jQuery。不需要 add jquery 到流星,因为它预先打包为对所有 MeteorJS 应用程序的硬依赖。
猜你喜欢
  • 1970-01-01
  • 2013-01-18
  • 2012-10-20
  • 1970-01-01
  • 2014-04-21
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
相关资源
最近更新 更多