0
我已經做了以下的自定義組件爲我knockout.js項目:Knockout.js通過PARAMS自定義組件
呈現以下文字:
<div class="alert alert-dismissible" data-bind="css:type" >
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<!-- ko if: type === 'alert-success' -->
<span class="glyphicon glyphicon-ok"></span>
<!-- /ko -->
<!-- ko if: type === 'alert-danger' -->
<span class="glyphicon glyphicon-remove"></span>
<!-- /ko -->
<p data-bind="text:message"></p>
</div>
組件從使用以下視圖模型:
define(['jquery','knockout','compMessage'],function($,ko,messageConstants){
function NotificationPopUpsViewModel()
{
var self=this;
self.notifications=ko.observableArray([]);
self.NOTIFICATION_INFO=function(){
return messageConstants.INFO();
};
self.NOTIFICATION_SUCCESS=function(){
return messageConstants.SUCCESS();
};
self.NOTIFICATION_FAIL=function(){
return messageConstants.FAIL();
};
function NotificationPopUp(message,type)
{
var notification=this;
notification.type=ko.observable(type);
notification.message=ko.observable(message);
}
self.addNotification=function(nessage,type,ttl){
var notificationPopUp=new NotificationPopUp(message,type);
ttl=ttl||2000;
self.notifications.push(notificationPopUp);
// setTimeout(function(){
// self.notifications.remove(notificationPopUp);
// },ttl);
};
}
return NotificationPopUpsViewModel;
})
我想整體呈現的html片段是:
<div id="message-area" class="col-xs-8" data-bind="with:notificationPopUpsViewModel">
<!-- ko foreach: notifications -->
<message params="this"></message>
<!-- /ko -->
</div>
但由於某種原因,我無法設法正確地將參數傳遞給我的視圖模型。