首页 \ 问答 \ knockoutjs中的双向绑定(Two way binding in knockoutjs)

knockoutjs中的双向绑定(Two way binding in knockoutjs)

我刚刚开始使用knockoutjs。 在下面的代码中我只想尝试双向绑定DIV的宽度。

var ViewModel = function () {
    this.width = ko.observable(7);
};

ko.bindingHandlers.widthBinding = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var div = $(element);
        var value = valueAccessor();
        var Width = ko.utils.unwrapObservable(value);
        div[0].style['width'] = Width + "px";
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var value = valueAccessor();
        var Width = ko.utils.unwrapObservable(value);
        div[0].style['width'] = Width + "px";
    }
};
$("#contentDiv").enableResize();
ko.applyBindings(new ViewModel());

<input data-bind="value: width" />
<div id="contentDiv"  data-bind="widthBinding : width" >

在上面的代码中,我有两个UI元素,一个是文本输入,另一个是DIV。我们可以在运行时调整DIV的大小。 如果我在文本输入中输入一些数字将适用于DIV的宽度,那就行了。 同时,如果我在运行时调整DIV大小,则意味着其宽度应反映在文本输入中。 有没有办法做到这一点?


I just now start using knockoutjs. In below code am just trying to bind DIV's width in two-way.

var ViewModel = function () {
    this.width = ko.observable(7);
};

ko.bindingHandlers.widthBinding = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var div = $(element);
        var value = valueAccessor();
        var Width = ko.utils.unwrapObservable(value);
        div[0].style['width'] = Width + "px";
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var value = valueAccessor();
        var Width = ko.utils.unwrapObservable(value);
        div[0].style['width'] = Width + "px";
    }
};
$("#contentDiv").enableResize();
ko.applyBindings(new ViewModel());

<input data-bind="value: width" />
<div id="contentDiv"  data-bind="widthBinding : width" >

In the above code i have two UI elements, one is Text Input and another one is DIV.And we can able to resize that DIV during run time. If i enter some number in text input means that will apply to DIV's width, that is working fine. At the same time if i resize the DIV during run time means its width should reflect in to the text input. Is there any way to do that?


原文:https://stackoverflow.com/questions/9017168
更新时间:2022-07-11 18:07

最满意答案

米奇说,没有。

实际上,Microsoft实际上建议在许多小型装配体上创建较少数量的较大尺寸的装配体。 此早期问题提供了非常有用的信息和参考。

在你的情况下,你绝对应该测试加载时间并将大小保持在可接受的范围内,但似乎有更多的问题与创建大量小组件然后几个大组件相关联。


As Mitch says, no.

In fact it is actually recommended by Microsoft to create a smaller number of larger sized assemblies over many small assemblies. This earlier question provides very useful information and references.

In your case you should definitely test the load times and keep the size within acceptable limits, but it seems there are more problems associated with creating numerous small assemblies then a few large ones.

相关问答

更多
  • 这是一个LoadLibrary()失败。 听起来你的插件有一些依赖于一些非托管的DLL。 是的,Windows将很难找到这些DLL,它没有理由查看插件目录。 也许这是第一次,因为你的应用程序的默认目录恰好设置为正确的目录。 那么这也将是解决方法,使用Environment.CurrentDirectory。 找出哪些依赖关系找不到是关键。 未管理的程序不会显示在fuslogvw.exe中,您可以从SysInternals的ProcMon实用程序中找到它。 This is a LoadLibrary() fa ...
  • 如果所有函数都具有相同的签名,请考虑使用函数指针填充map或类似数据结构,并将其作为查找关键字进行切换。 没有人能够理解2500个开关语句,所以我建议在你的代码中不要有一个开关语句。 With The intial trials i came to conclusion that theres nothing much when we try with Function pointers.. i found contiguous switch cases and function pointers are ...
  • 当您在VB6中编写代码时,编译结果是一个COM组件。 COM组件提供了通常使用COM类型库描述的接口,coclasses,结构体和枚举。 但是,要在.NET中使用COM组件,您需要以.NET所理解的格式(即.NET程序集(因为它不能直接用于类型库))的类型描述。 因此,互操作程序集只是一个“转换的”COM类型库,其意义在于它包含对应于类型库中相同内容的接口,结构体等的描述。 (以上是有些简化的,因为互操作程序集不需要从类型库生成 - 例如,如果需要,可以手动编码一个。) 与通常所说的相反,互操作程序集不包含 ...
  • 米奇说,没有。 实际上,Microsoft实际上建议在许多小型装配体上创建较少数量的较大尺寸的装配体。 此早期问题提供了非常有用的信息和参考。 在你的情况下,你绝对应该测试加载时间并将大小保持在可接受的范围内,但似乎有更多的问题与创建大量小组件然后几个大组件相关联。 As Mitch says, no. In fact it is actually recommended by Microsoft to create a smaller number of larger sized assemblies o ...
  • 我刚刚在命名空间中创建了一个带有10.000个类的小型测试项目,虽然在加载/ jitting组件时有明显的开销,但我不会说它特别慢。 因此,可能不是类本身的数量,这是你所看到的糟糕表现的原因。 我是乔恩在这里,对你的测试应用程序有更多信息会很有帮助。 I just created a small test project with 10.000 classes in a namespace and while there's a noticeable overhead when loading/jittin ...
  • 命名空间和程序集(DLL)都提供了对项目进行分区的方法,但它们以不同的方式执行:命名空间提供逻辑分区,而程序集提供物理分区。 通常,这些分区的边界完全匹配,但不一定如此:您可以让同一名称空间中的类出现在多个程序集中; 您还可以将多个名称空间中的类放入同一个程序集中。 创建我遵循的新程序集的经验法则很简单:如果存在一组类可以独立于另一组类使用的情况,则这两个组应该转到单独的程序集。 这使您可以更灵活地将程序集混合为其他项目的依赖项。 由于依赖项的依赖项是懒惰加载的,因此使用较小的DLL可以更好地管理应用程序的 ...
  • 您的操作系统库可能具有强制终止线程的功能,但这是一个坏主意,除非您完全确定这些DLL正在做什么。 在Windows上,这将是TerminateThread 。 对于POSIX,我相信它是pthread_cancel 。 从外部终止线程的问题是线程所持有的任何资源都将被泄露。 例如,线程打开的任何打开文件都将保持打开状态,并且它所做的任何分配都将泄漏。 最好确保你的DLL永远不会卡住。 我会记录一些东西并调试问题发生的地点和原因,而不是终止线程。 Your operating system's librari ...
  • 好的线索似乎是在callstacks中。 在“删除”调用堆栈中,您会注意到它直接在mydll.dll中调用各种删除函数等。 在分配中,分配由msvcr90d.dll执行。 你所拥有的是你的exe上设置的/ MDd(或/ MD中的释放)标志和你的dll上设置的/ MTd(或释放中的/ MT)。 将它们设置为/ MDd(或发布中的/ MD)并且您的问题将消失...基本上您将设置exe和dll来调用CRT dll,而不是尝试执行2个不同的操作方法 ... Well the clue appears to be i ...
  • 您只能加载与应用程序进程的位数相匹配的DLL。 这就是BadImageFormatException试图告诉您的,您试图加载不兼容的DLL。 因此,如果您有32位(x86)进程,则只能加载32位(x86)DLL。 如果您有64位(x64)进程,则只能加载64位(x64)DLL。 “任何CPU”编译器设置只意味着进程的位数将与您的计算机的本机位数相匹配:64位操作系统上为64位,否则为32位。 您需要获取与应用程序的位数相匹配的DLL版本,或者重新编译应用程序以定位不同的位数。 The problem was ...
  • 从您的描述中,您似乎已经知道如何让目标进程加载您的DLL。 如果我的假设是正确的,那么答案很简单:从DLLMain创建一个线程并在线程中实现你的逻辑。 只要您的代码遵守下面列出的规则,您就可以了。 本文档描述了DLLMain中可以做什么和不可以做什么以及为什么。 如文档所述,您不应该在DllMain中执行以下任务: 调用LoadLibrary或LoadLibraryEx(直接或间接)。 这可能会导致死锁或崩溃。 与其他线程同步。 这可能会导致死锁。 获取由等待获取加载程序锁定的代码所拥有的同步对象。 这可能 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)