我正在尝试使用ractive.on和ractive.set更改JSON对象内部的值。基本上,它将在输入元素内获取值(带有有效变量),并在通过on-click事件按下锚元素后将它们分配给相应的键。没有显示正确的值,而是显示Promise。我已附上图片以供参考。

https://jsfiddle.net/clestcruz/L38tsud7/

javascript - 激活ractive.set后,键将Promise显示为一个值-LMLPHP

<body>

  <div id='container'></div>

  <script id='template' type='text/ractive'>

      <p>Provide the company information for the:</p>

      <label>Company Name <span class="required">*</span></label>
      <input type="text" class="form-text full-width" placeholder="Enter the company name" id="accountName" value="{{leadData.accountName}}"/>



      <label>Street Name.</label>
      <input type="text" class="form-text" placeholder="-" id="streetName" value="{{leadData.streetName}}"/>

      <a class="button cta" on-click="addLead">Create Account</a>


  </script>

  <script src='http://cdn.ractivejs.org/latest/ractive.min.js'></script>

  <script>

      var app = new Ractive({
        el       : '#container',
        template : '#template',
    });

     var data = {
        'accountName'   : app.get('leadData.accountName'),
        'streetName'    : app.get('leadData.streetName'),
     }


      app.on('addLead', function(event) {
         var data = {
           'accountName'   : app.set('leadData.accountName'),
           'streetName'    : app.set('leadData.streetName'),
         }
         console.log(data);
      });

  </script>


</body>

最佳答案

您可能的意思是app.get而不是app.set。另外,事件处理程序在实例的上下文中执行。这意味着您不需要使用外部变量(在本例中为app)引用实例。您可以简单地使用this

app.on('addLead', function(event) {
  var data = {
    accountName: this.get('leadData.accountName'),
    streetName: this.get('leadData.streetName'),
  }
  // Do other stuff with data
});

关于javascript - 激活ractive.set后,键将Promise显示为一个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41661198/

10-11 08:22