2015-02-18 3 views
1

Я пытаюсь вставить нормальное выражение JavaScript в файл Jade. Но мое приложение продолжает получать сообщение об ошибке, когда я добавляю переменную isAccepted в свой код. Это сообщение об ошибке.Невозможно вставить переменную JavaScript в Jade

Your app is crashing. Here's the latest log.

Errors prevented startup:

While building the application: jade-test.jade: Jade syntax error: Expected identifier, number, string, >boolean, or null {{isAccepted ? 'class1' : 'class2... ^ packages/compileJade/plugin/handler.js:44:1: Cannot read property 'head' >of undefined (compiling jade-test.jade) (at fileModeHandler)

Your application has errors. Waiting for file change.

Это мой код:

нефрита test.jade

template(name="layout") 
    h1 This is layout template 
    h2 This is h2 paragraph 
    h3 This is h3 paragraph 
    +hello 

template(name="hello") 
    h4 This is home template 
    button Click me 
    p You have click me #{counter} times. 
    - var isAccepted = true 
    button(class=isAccepted ? 'class1' : 'class2') I'm the right choice. 

нефрита-test.js

if (Meteor.isClient) { 
    // counter starts at 0 
    Session.setDefault('counter', 0); 

    Template.hello.helpers({ 
    counter: function() { 
     return Session.get('counter'); 
    } 
    }); 

    Template.hello.events({ 
    'click button': function() { 
     // increment the counter when button is clicked 
     Session.set('counter', Session.get('counter') + 1); 
    } 
    }); 

    Router.route('/', function() { 
    this.render('layout'); 
    }); 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    // code to run on server at startup 
    }); 
} 

ответ

0

Не уверен, что если вставить код яваскрипта, как это в нефритом поддерживается, вам, скорее всего, следует использовать помощника.

Я не думаю, что синтаксис тернарного выражения также действителен, опять же, используйте помощник.

JADE

template(name="hello") 
    h4 This is home template 
    button Click me 
    p You have click me #{counter} times. 
    button(class=isAccepted) I'm the right choice. 

JS

Template.hello.helpers({ 
    isAccepted: function(){ 
    var isAccepted = true; 
    return isAccepted ? "class1" : "class2"; 
    } 
}); 
+0

@ saimeunt, я следую за руководство с этой страницы: http://jade-lang.com/reference/attributes/. Который говорит: все нормальные выражения JavaScript тоже работают отлично: '- var authenticated = true body (class = authenticated? 'Authed': 'anon')' – Nguyenducthanh

+0

Я думаю, что «meteor-jade» - это всего лишь подмножество всего синтаксиса Jade и вы не можете использовать эту функцию в любом случае. – saimeunt

+0

Большое спасибо. Я буду использовать другой синтаксис. – Nguyenducthanh

Смежные вопросы