2014-02-19 52 views
1

省略keydown input : function(event) {}给我沿着 “While building the application: client/client.js:33:11: Unexpected token (” 的线,基本上是起点的错误。我想知道为什么我一开始就需要javascript函数。不要得到错误。这是一个问题,尤其是因为我不希望点击功能在每次按下按键时都运行。无论如何,要弄清楚如何在这里使用jQuery而不是javascript或者更改keydown输入是非常好的。使用jQuery与流星捐赠错误

Template.create_poll.events = { 

    'keydown input' : function(event) { 

    $("input").keypress(function() { 
     var active_element = $(this).parent().attr("id"); 
     var last_child = $('ul li:last').attr("id"); 
     var option_number_index = last_child.lastIndexOf("-"); 
     var option_number = last_child.substring(option_number_index+1); 
     option_number = option_number/1; 

     //console.log(option_number); 

     //console.log(last_child); 
     if (last_child == active_element) { 
     console.log(active_element); 
     option_number += 1; 
     console.log(option_number); 
     $('ul').append('<li id="poll-choice-' + option_number + '"><input name="choice" type="text" placeholder="Option ' + option_number + '">'); 
     } 
    }); 


    $("#poll_create").click(function() { 
     console.log("Button works"); 
     var choices = new Array(); 
     var counter = 0; 
     $("ul li input").each(function() { 
     choices[counter] = $(this).val(); 
     counter++; 
     }); 

     console.log(choices[1]); 
     console.log(choices[5]); 

    }); 

    } 



} 
+2

您试图混合使用jQuery事件代码和Meteor事件代码。当按照预期使用Meteor时,您应该发现自己使用的是非常少的jQuery代码,因为Meteor正在幕后处理低级jQuery。你应该只是将你的事件实现为''keyup input''并且''点击#poll_create'' – sbking

+0

我还能像'$(this).parent()。attr(“id”);''那样使用jQuery语法吗? – Maaz

+0

流星等值将是'event.currentTarget.parentNode.id'。请注意,这是使用默认的DOM代码。我认为即将发布的新流星模板引擎使用jQuery,所以它可能会变得更像你正在做的事情。我强烈推荐阅读[流星文档](http://docs.meteor.com/)*从上到下*。它解释了很多这些事情。一旦你了解Meteor的工作方式,你应该发现自己几乎不需要任何jQuery语法。流星为你做jQuery的东西。 – sbking

回答

1

Template.create_poll.events期望一个eventMap是:

事件映射是一个对象,其中属性指定一组事件的处理,并且该值是针对这些事件的处理程序。该属性可以是以下几种形式之一:

因此,您需要传入'keydown input' : function (event, templ) { ... }以使其成为有效的Javascript对象。

在这种情况下,你应该使用流星的事件地图关注@ Cuberto的建议和实施的事件:

Template.create_poll.events = { 

    'press input' : function(event) { 
     var active_element = $(this).parent().attr("id"); 
     var last_child = $('ul li:last').attr("id"); 
     var option_number_index = last_child.lastIndexOf("-"); 
     var option_number = last_child.substring(option_number_index+1); 
     option_number = option_number/1; 

     //console.log(option_number); 

     //console.log(last_child); 
     if (last_child == active_element) { 
     console.log(active_element); 
     option_number += 1; 
     console.log(option_number); 
     $('ul').append('<li id="poll-choice-' + option_number + '"><input name="choice" type="text" placeholder="Option ' + option_number + '">'); 
     } 
    }, 
    'click #poll_create' : function (event) { 
     console.log("Button works"); 
     var choices = new Array(); 
     var counter = 0; 
     $("ul li input").each(function() { 
     choices[counter] = $(this).val(); 
     counter++; 
     }); 

     console.log(choices[1]); 
     console.log(choices[5]); 

    } 
} 

但是,如果你想使用某些jQuery的特定事件,那么你可以在rendered它们附加功能:

Template.create_poll.rendered = function() { 
    $("input").keypress(function() { 
     var active_element = $(this).parent().attr("id"); 
     var last_child = $('ul li:last').attr("id"); 
     var option_number_index = last_child.lastIndexOf("-"); 
     var option_number = last_child.substring(option_number_index+1); 
     option_number = option_number/1; 

     //console.log(option_number); 

     //console.log(last_child); 
     if (last_child == active_element) { 
     console.log(active_element); 
     option_number += 1; 
     console.log(option_number); 
     $('ul').append('<li id="poll-choice-' + option_number + '"><input name="choice" type="text" placeholder="Option ' + option_number + '">'); 
     } 
    }); 


    $("#poll_create").click(function() { 
     console.log("Button works"); 
     var choices = new Array(); 
     var counter = 0; 
     $("ul li input").each(function() { 
     choices[counter] = $(this).val(); 
     counter++; 
     }); 

     console.log(choices[1]); 
     console.log(choices[5]); 

    }); 
}; 
+0

这种方法意味着我不能使用任何jQuery语法,包括'$(this).val()'之类的东西,我该如何解决这个问题? 在另一个笔记上,'rendered'和'event'之间的速度有什么区别,通常是处理像这样的事件的更可接受的方式。 – Maaz

+1

流星事件处理函数获取两个参数:'event'和'template'。 'template'参数是一个[模板实例](http://docs.meteor.com/#template_inst)对象,它是一个'find(selector)'方法,其作用类似于'$(selector)',但是作用域到模板本身并返回一个常规DOM'HTMLElement'对象。所以你可以做'template.find(“input”).value' – sbking