2017-09-27 49 views
-2

我想要做的就是調用一個函數如下,進一步我想通過當前元素(項目)作爲參數。與骨幹鉚釘:href/onclick屬性調用方法(函數)

<tr rv-each-item="items:models"> 
    <td><a href="javascript:selectedItem(item);">{ item:Name }</a></td> 
</tr> 

var selectedItem = function (item) 
{ 
    console.log(item); 
} 

,同時摸索,我發現下面的討論有所幫助,但因爲它沒有實現骨幹

https://github.com/mikeric/rivets/issues/554

Rivets.js: When button is clicked, call a function with an argument from a data binding

+0

我寫了一個你分享的問題的答案。過去我曾使用鉚釘與骨幹。請分享一個[mcve],說明爲什麼它不適用於主幹。你可能做錯了什麼。 –

回答

0

工作時左右,我發現不同的方法可以解決不了我的問題幫助,張貼在這裏如果有人可以得到幫助或改善,如果有任何事情需要。

選項1

<body> 
    <div rv-each-book="model.books"> 
     <button rv-on-click="model.selectedBook | args book"> 
      Read the book {book} 
     </button> 
    </div> 
</body> 

<script type="text/javascript"> 
    rivets.formatters["args"] = function (fn) { 
     var args = Array.prototype.slice.call(arguments, 1); 
     return function() { 
      return fn.apply(null, args); 

     }; 
    }; 

    rvBinder = rivets.bind(document.body, { 
     model: { 
      selectedBook: function (book) { 
       alert("Selected book is " + book); 
      }, 
      books: ["Asp.Net", "Javascript"] 
     } 
    }); 
</script> 

選項2 創建一個定製綁定

<body> 
    <div rv-each-book="books"> 
     <a rv-cust-href="book"> 
      Read the book {book} 
     </a> 
    </div> 
</body> 

<script type="text/javascript"> 

    rivets.binders['cust-href'] = function (el, value) { 
     //el.href = '/Books/Find/' + value; 
     //OR 
     el.onclick = function() { alert(value);}; 
    } 

    rvBinder = rivets.bind(document.body, { 
      books: ["Asp.Net", "Javascript"] 
    }); 
</script> 

選項3 當我使用與骨幹rivetsjs,我也能獲得優勢骨幹視圖上的事件

// backbone view 
events: { 
    'click #linkid': 'linkclicked' 
}, 
linkclicked: function (e){ 
    console.log(this.model.get("Name")); 
}, 

<td><a id="linkid" href="#">{ item:Name }</a></td>