1

我正在尝试让 Knockout.js 与 Drupal 一起使用。似乎 Drupal 模板文件中的 Knockout 调用没有读取我的任何 Knockout 调用。有没有人让这两个一起工作?

4

1 回答 1

0

你可能忘记了这一切,但我通过在页脚中包含 js 内联来让它工作:

$header = array(
 'type' => 'file'
 );

 $footer = array(
 'type' => 'file',
 'scope' => 'footer'
 );


drupal_add_js(drupal_get_path('theme', 'MyTheme'). '/js/knockout.js', $header);


<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<h2>Hello, <span data-bind="text: fullName"> </span>!</h2>


 drupal_add_js('var ViewModel = function(first, last) {
     this.firstName = ko.observable(first);
     this.lastName = ko.observable(last);

     this.fullName = ko.computed(function() {
         // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
         return this.firstName() + " " + this.lastName();
     }, this);
 };

 ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work

 ',
     array('type' => 'inline', 'scope' => 'footer', 'weight' => 1)
   );

如果您从文件中找到了一种方法,请告诉我,这样做是没有好处的。

编辑:没关系,如果您在页脚包含文件,它会起作用:

drupal_add_js(drupal_get_path('theme', 'MyTheme'). '/js/MyKnockoutScript.js', $footer);
于 2013-09-05T03:52:18.573 回答