2017-06-22 23 views
1
    @{ Html.RenderPartial(MVC.Shared.Views.Partials.CameraTagVideoPlayer, new CameraTagVideoPlayerViewModel("applicationVideo", "xxx")); } 

我想用特定的敲除数据绑定替换“xxx”。如何在html renderPartial中替换字符串?

例如,

<span data-bind="text: application.applicationKey"></span> 

给了我一个人的applicationKey。我想把这个键放在RenderPartial里面来播放视频。有没有办法做到这一点?

编辑:

public static class Partials 
{ 
    public readonly static string _CurvedSelector = "~/Views/Shared/Partials/_CurvedSelector.cshtml"; 
    public readonly static string SelectMonthOptions = "~/Views/Shared/Partials/SelectMonthOptions.cshtml"; 
    public readonly static string SelectStateOptions = "~/Views/Shared/Partials/SelectStateOptions.cshtml"; 
    public readonly static string SelectYearOptions = "~/Views/Shared/Partials/SelectYearOptions.cshtml"; 
    public readonly static string ToggleButton = "~/Views/Shared/Partials/ToggleButton.cshtml"; 
    public readonly static string CameraTagVideoPlayer = "~/Views/Shared/Partials/CameraTagVideoPlayer.cshtml"; 
    public readonly static string CreditCardFormFields = "~/Views/Shared/Partials/CreditCardFormFields.cshtml"; 
    public readonly static string CreditCardJavascript = "~/Views/Shared/Partials/CreditCardJavascript.cshtml"; 
    public readonly static string AntiBotFormFields = "~/Views/Shared/Partials/AntiBotFormFields.cshtml"; 
    public readonly static string ExitModal = "~/Views/Shared/Partials/ExitModal.cshtml"; 

} 

cameratagvideoplayer.html:

@model CameraTagVideoPlayerViewModel 

@{ 
    // CameraTag video security 
    long expiration = Utilities.ToUnixTimestamp(DateTime.UtcNow.AddMinutes(30)); // can be no more than 1 hour 
    string signature = Utilities.CreateTokenHmacSha1(expiration.ToString(), AppSettings.Current.CameraTagRestApiKey); 
} 

<player id="@Model.Id" 
     data-uuid='@(Model.VideoUuid)' 
     data-options='{"width":"100%","height":"100%", "aspectratio":"4:3", "displaytitle":"false","displaydescription":"false"}' 
     data-signature="@signature" data-signature-expiration="@expiration"></player> 
+0

你能否显示部分视图'MVC.Shared.Views.Partials.CameraTagVideoPlayer'? –

+0

@JoseLuis刚刚添加!这有帮助吗? – Dukakus17

回答

1

如果淘汰赛被启用时,这一观点被redered,你可以使用attr绑定(http://knockoutjs.com/documentation/attr-binding.html):

<player id="@Model.Id" 
    data-bind="attr: { 'data-uuid': application.applicationKey }" 
    data-options='{"width":"100%","height":"100%", "aspectratio":"4:3", "displaytitle":"false","displaydescription":"false"}' 
    data-signature="@signature" 
    data-signature-expiration="@expiration"> 
</player> 

希望这有助于。

更新1 这个问题是关于相机标签(https://cameratag.com/)。这个JavaScript库发现了带有onload事件的<player>标签。您可以添加新标签,但无法修改现有标签。

您可以创建一个自定义绑定,每个变化,创建一个新的<player>标签(当然,删除旧的):

ko.bindingHandlers.uuid = { 
    update: function(element, valueAccessor, allBindings) { 
     // First get the latest data that we're bound to 
     var value = valueAccessor(); 
//console.log(allBindings()) 
     // Next, whether or not the supplied model property is observable, get its current value 
     var valueUnwrapped = ko.unwrap(value); 
     var parentId = `${$(element).attr('id')}` 
     var childId = `${parentId}_child` 
     var childIdHash = `#${parentId}_child` 

     // Delete de old <player> 
     $(childIdHash).remove(); 

     var player = $('<player></player>') 
     .attr('id',childId) 
     .attr('data-uuid',valueUnwrapped) 
     .insertAfter($(element)) 

     $.each($(element).data(),function (key, value) { 
      if (key !== 'bind' && key !== 'options'){ 
      var temp = value;    

      if (typeof(value) !== 'string'){ 
       // For example, data-options: 
       // data-options='{"width":"100%","height":"100%", "aspectratio":"4:3", "displaytitle":"false","displaydescription":"false"}' 
       temp = {} 
       $.each(value,function(key1,value1){ 
       temp[key1] = value1; 
       }) 
      } 

      player.attr(`data-${key}`,temp); 

      console.log(`x) ${key} : ${value}`); 
      console.log(value) 
      } 
     }) 

     CameraTag.setup(); 
    } 
}; 

这种结合也复制原始元素的data-...属性。

然后,它可以这样使用:

<player id="DemoPlayer" 
    data-bind="uuid: uuid" 
    data-options='{"width":"100%","height":"100%", "aspectratio":"4:3", "displaytitle":"false","displaydescription":"false"}' 
    data-signature="@signature" 
    data-signature-expiration="@expiration"> 
</player> 

和视图模型:

function ViewModel() { 
    var self = this; 
    self.uuid = ko.observable('v-1237c41f-9038-44b7-b4cc-8cb1f13f6f76') 
} 

ko.applyBindings(new ViewModel()); 

这里是一个fiddle一起玩。在编辑框中,您可以更改密钥。这个例子的关键是在这个url中找到:player example

+0

这很奇怪。它确实给了我data-uuid = applicationKey绑定,但视频不显示。任何想法? – Dukakus17

+0

@ user7677413我更新了答案。您用来播放音频的库在加载时找到了''标签。因此,当Knockout提供该值时,相机标签不会看到它,因为它尚未扫描此''。我添加了一个自定义绑定,每次由Knockout处理时都创建一个新的''。我添加一个小提琴玩。希望这可以帮助。 –

+1

你是我的英雄!谢谢! – Dukakus17