我试图创建一个淘汰绑定到jquery ui可调整大小的小部件.我的自定义绑定绑定到视图模型上的2个不同的可观察对象,即"left"和"width".
<div class="layer" data-bind="resizable: {left: left, width: width}">
<div class="left ui-resizable-handle ui-resizable-w"><<<</div>
<div class="right ui-resizable-handle ui-resizable-e">>>></div>
</div>
ko.bindingHandlers.resizable = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
var values = valueAccessor();
$(element).resizable({
handles: null,
resize: function(event, ui) {
values.left(ui.position.left);
values.width(ui.size.width);
}
});
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
var values = valueAccessor();
$(element)
.css('left', values.left() + 'px')
.css('width', values.width() + 'px')
;
}
};
var vm = {
left: ko.observable(50),
width: ko.observable(300)
};
ko.applyBindings(vm);
Run Code Online (Sandbox Code Playgroud)
问题是,当我使用左句柄调整div时,2个observable会更新,从而在同一个绑定中触发2次更新.我需要它才能触发一次更新.
我对这种绑定有什么选择?创建绑定到多个observable的绑定的推荐方法是什么?
我的真实世界代码更复杂,但我认为iv将我的问题归结为这个小提琴:
根据这个答案Knockout.js:时间输入格式和值限制,我正在尝试创建一个自定义绑定,如果值为字符串为空,则将observable设置为null,这里的代码不起作用,Ip observable始终为null模型
ko.bindingHandlers.stringEmptyNull = {
init: function (element, valueAccessor, allBindingsAccessor) {
var underlyingObservable = valueAccessor();
var interceptor = ko.dependentObservable({
read: underlyingObservable,
write: function (value) {
if (value != null && value.trim() == '')
underlyingObservable();
}
});
ko.bindingHandlers.value.init(element, function () { return interceptor }, allBindingsAccessor);
},
update: ko.bindingHandlers.value.update
};
Run Code Online (Sandbox Code Playgroud)
输入:
<input type="text" data-bind="stringEmptyNull: Ip" />
Run Code Online (Sandbox Code Playgroud)
模型:
var Model = function () {
this.Ip = ko.observable()
ko.applyBindings(this, $myForm[0]);
};
instance = new Model();
Run Code Online (Sandbox Code Playgroud) 我需要能够使用Asp.Net Core 2.0中的自定义绑定在客户端中发送授权。它可以在Asp.net 4.6.1中工作,而不能在Core 2.2中工作。我正在尝试连接到我们租户中的Workday公用Web服务。由于我能够在Asp.Net 4.6.1中进行此工作,因此我在此完成了开发,但希望为将来的可能发展弄清楚。
我创建了我在代码中使用的自定义绑定。参见下文,但是,我总是收到此错误:完整消息:
System.PlatformNotSupportedException:不支持TransportSecurityBindingElement.BuildChannelFactoryCore。
我在Asp.Net Core 2.0 MVC中的实现:
public async Task<bool> ImportTimeEntryBlockAsync(TimeEntry item)
{
bool isValid = true;
//Create the update object to update the webservice
//setup Header
Workday_Common_HeaderType header = new Workday_Common_HeaderType
{
Include_Reference_Descriptors_In_Response = true
};
//setup reported time block data from item
Reported_Time_Block_DataType timeBlockData = new Reported_Time_Block_DataType();
PopulateTimeBlock(item, ref timeBlockData);
Reported_Time_Block_DataType[] timeBlocks = new Reported_Time_Block_DataType[1];
timeBlocks[0] = timeBlockData;
//setup import reported time block request
Import_Reported_Time_Blocks_RequestType request = new Import_Reported_Time_Blocks_RequestType
{
version = "v29.0", …Run Code Online (Sandbox Code Playgroud) 我有一个这样的模型:
public class MainModel
{
public string Id {get;set;}
public string Title {get;set;}
public TimePicker TimePickerField {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
TimePicker 是一个内部模型,看起来像这样:
public class TimePicker
{
public TimeSpan {get;set;}
public AmPmEnum AmPm {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试为内部模型创建自定义模型绑定: TimePicker
问题是:如何在表单中提交到TimePicker模型字段的自定义模型绑定器中获取值?
如果我试着像这样:
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
Run Code Online (Sandbox Code Playgroud)
我刚刚得到null在value.
我不确定如何正确实现模型绑定器.
public class TimePickerModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
var result = new TimePicker();
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); …Run Code Online (Sandbox Code Playgroud) custom-binding custom-model-binder asp.net-mvc-3 asp.net-mvc-2
我最近遇到了一个问题,虽然我为我解决了这个问题,但我不确定那里是否有更好的解决方案,所以我很感激任何评论.
问题.我想创建一个'ScrollIntoView'绑定.由于将一个元素滚动到视图中,需要DOM-Element,我编写了一个自定义绑定,然后我想明确地触发它,只要我满意.我从这段代码开始:
ko.bindingHandlers.scrollTo = {
update: function (element, valueAccessor, allBindings) {
var _value = valueAccessor();
var _valueUnwrapped = ko.unwrap(_value);
if (_valueUnwrapped) {
element.scrollIntoView();
}
}
Run Code Online (Sandbox Code Playgroud)
};
绑定:
<div data-bind="scrollTo: goToThis">
Run Code Online (Sandbox Code Playgroud)
在ViewModel中我有这个可观察的:
_self.goToThis = ko.observable(false).extend({notify: 'always'});
Run Code Online (Sandbox Code Playgroud)
然后我可以通过调用触发:
_self.goTohis(true);
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.但是我很快遇到了问题.因为每当我将goTothis()Observable设置为true时,真正的值就会停留在它上面,导致一些元素滚动到视图中,而用户没有明确地触发它.例如,当我更改视图时,基本上用if绑定隐藏所有元素,然后切换回来,if绑定将重新触发之前已设置为true的所有goToThis observable.啊!
所以我想到了这个模式并扩展了我的custum绑定,如下所示:
ko.bindingHandlers.scrollTo = {
update: function (element, valueAccessor, allBindings) {
var _value = valueAccessor();
var _valueUnwrapped = ko.unwrap(_value);
if (_valueUnwrapped) {
element.scrollIntoView();
// resets the trigger value to false. Otherwise there will be more and more ViewModels, where the value is …Run Code Online (Sandbox Code Playgroud) 我想问你是否可以格式化动态输入值?
因此,当我输入一些值时,它将动态添加逗号以格式化货币.
例如,我正在编写1234,它将被动态格式化为1'234.
如果我删除一个数字,它将被更改为123.
我正在考虑一些自定义绑定.但是,是否可以为每次更改添加规则?
欢呼
这是真的:(或者我错过了什么?)
如果是,如何定义自定义css绑定?
我在这里找到了一个实现,但我只有缩小的kendo js文件,任何人都可以提供下载未压缩的kendo js文件的链接吗?
更新
临时解决方案:http://feedback.kendoui.com/forums/127393-kendo-ui-feedback/suggestions/2782980-add-an-mvvm-css-binding
必须修改kendo.web.js源代码.
我正在尝试编写一个自定义绑定,将" /n" 替换<br />为" <p>"元素中的" ".
我或多或少地理解这个概念,但我正在努力让它继续下去.任何人都可以建议我哪里出错了.我真的不想使用计算的observable,因为我想使用" /n"而不是" <br />" 保持实际值.
ko.bindingHandlers.nl2br = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var field = valueAccessor();
field.replace(/\n/g, '<br />');
$(element).val(field)
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var field = valueAccessor();
field.replace(/\n/g, '<br />');
$(element).val(field)
}
};
Run Code Online (Sandbox Code Playgroud) 我有一个Knockout自定义绑定处理程序,我想在其中调用foreach绑定功能,然后调用回调函数.我一直得到一个"未捕获的错误:您不能多次将绑定应用于同一个元素."现在我尝试这样做时出错.
我的自定义绑定非常简单(typescript):
/// <reference path="knockout.d.ts" />
ko.bindingHandlers["postForeach"] = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
if (!allBindingsAccessor().postForeachCallback)
throw "Callback not defined for postForeach binding!";
//call foreach init functionality
ko.bindingHandlers['foreach'].init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
//call foreach update functionality
ko.bindingHandlers['foreach'].update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
//call callback
allBindingsAccessor().postForeachCallback();
}
};
Run Code Online (Sandbox Code Playgroud)
在构建这个时我是否缺少一些东西?
谢谢!
编辑:
回调函数
self.populateMainContentWindow = function () {
var dataTable = $(this.tableId).dataTable();
dataTable.fnDestroy();
// create the datatable
var actualTable = this.jQuery(this.tableId); …Run Code Online (Sandbox Code Playgroud) 在WPF中,我经常使用绑定。
<GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}"/>
Run Code Online (Sandbox Code Playgroud)
是否有一个好的方法可以在淘汰赛中实现类似的约束?我正在使用一个sprintf库,它将很好地利用。
我想我可以为此创建一个自定义绑定,但这应该是一个相当普遍的请求,因此我认为在尝试重新发明轮子之前,我会在这里进行检查。
一种用例是动态构建标签的href属性a,例如,生成类似以下内容的代码:
<a href="#products/1/product/2">Foo</a>
Run Code Online (Sandbox Code Playgroud)
其中1是产品组,2是产品ID
简而言之:这是我用于将复选框置于不确定状态的淘汰赛自定义绑定。
ko.bindingHandlers.nullableChecked = {
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (value == null) element.indeterminate = true;
ko.bindingHandlers.checked.update(element, function () { return value; });
}
};
Run Code Online (Sandbox Code Playgroud)
如果初始值null一切正常并且复选框处于不确定状态,但是当我单击复选框时,它似乎没有相应地将绑定属性的值更新为 false/true。我错过了什么吗?
我有WCF服务.这是配置
<basicHttpBinding>
<binding name="EmergencyRegistratorBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)
和服务配置
<service behaviorConfiguration="Default" name="Breeze.AppServer.Emergencies.EmergencyRegistrator">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="EmergencyRegistratorBinding"
contract="Services.IEmergencyRegistrator" />
</service>
Run Code Online (Sandbox Code Playgroud)
一切都很好.但我需要将basicHttpBingind更改为DuplexBinding.我添加了延伸:
<extensions>
<bindingElementExtensions>
<add name="pollingDuplex" type="System.ServiceModel.Configuration.PollingDuplexElement, System.ServiceModel.PollingDuplex"/>
</bindingElementExtensions>
</extensions>
Run Code Online (Sandbox Code Playgroud)
并改变上面提到的行:
<customBinding>
<binding name="DuplexmergencyRegistratorBinding">
<binaryMessageEncoding/>
<pollingDuplex maxPendingSessions="2147483647" maxPendingMessagesPerSession="2147483647" inactivityTimeout="02:00:00" serverPollTimeout="00:05:00"/>
<httpTransport authenticationScheme="Negotiate"/>
</binding>
</customBinding>
Run Code Online (Sandbox Code Playgroud)
和
<service behaviorConfiguration="Default" name="Breeze.AppServer.Emergencies.EmergencyRegistrator">
<endpoint address="" binding="customBinding" bindingConfiguration="DuplexmergencyRegistratorBinding" contract="Breeze.Core.Services.IEmergencyRegistrator" />
<endpoint address="mex" binding="customBinding" bindingConfiguration="DuplexmergencyRegistratorBinding" contract="IMetadataExchange"/>
</service>
Run Code Online (Sandbox Code Playgroud)
我已将服务参考添加到WCF项目.引用已成功添加,但Reference.cs几乎为空.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.225
//
// Changes …Run Code Online (Sandbox Code Playgroud) custom-binding ×12
knockout.js ×8
javascript ×4
binding ×1
checkbox ×1
jquery ×1
kendo-ui ×1
mvvm ×1
soap-client ×1
triggers ×1
wcf ×1