knockout.js - 模板的延迟加载
因此,我来自一个模板工作流程,该工作流程涉及创建一个数据对象(类似于淘汰赛中的视图模型),将其传递给模板引擎(在我们的例子中为 jstemplate),使用该数据对象渲染模板,并将其附加到 dom。
如何通过淘汰赛实现类似的工作流程? “if”控制流是我正在寻找的吗?或者将我的模板粘贴在没有数据绑定属性的脚本标记中,然后动态添加它们并像 ko.applyBindings(viewModel, node) 一样处理模板?
我很好奇其他人如何使用淘汰赛来延迟加载模板。
另外,如果你能告诉我为什么下面的 js 小提琴不能像我期望的那样工作,那就额外加分了。我正在尝试学习 if 控制流绑定,但这不起作用。
So I come from a templating workflow that involves creating a data object (akin to a view model in knockout) passing that to a templating engine (jstemplate in our case), rendering the template using that data object, and appending it to the dom.
How do I achieve a similar work flow with knockout? Is the "if" control flow what I'm looking for? Or sticking my templates in script tags without data-bind attributes and adding them dynamically later and processing the template like ko.applyBindings(viewModel, node)?
I'm curious how others lazy load templates using knockout.
Also, extra credit if you can tell me why the js fiddle below doesn't work as I would expect it to. I'm trying to learn the if control flow binding and this doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以采取以下几个方向:
您可以将不同的视图模型应用于不同的元素,正如您所提到的:
您甚至可以将其数据的绑定动态地应用到元素,例如:
就像这个示例:< a href="http://jsfiddle.net/rniemeyer/gYk6f/" rel="noreferrer">http://jsfiddle.net/rniemeyer/gYk6f/
您还可以执行以下操作:
然后,绑定它就像:
它们甚至可以像这样嵌套:
因为模型是可观察的,所以它们最初可以是空的,并根据需要填充。
在这些情况下,您当然也可以使用命名模板,例如:
对于额外的学分问题:
p
标记不能包含其他块级元素(例如您的div
)。浏览器将其移出p
标记。将您的div
替换为span
,它将表现得像您期望的那样(或将p
替换为div
)。There are several directions that you can go for something like this:
you can apply different view models to different elements, as you mentioned like:
You can even dynamically apply a binding with its data to an element like:
Would be like this sample: http://jsfiddle.net/rniemeyer/gYk6f/
You can also do something like:
Then, bind it like:
They, can even be nested like:
Since, the models are observable, they can initially be empty and get populated on demand.
You can certainly use named templates in those cases as well like:
For the Extra Credit question:
p
tags cannot contain other block level elements (like yourdiv
). The browser moves it out of thep
tag. Replace yourdiv
with aspan
and it will behave like you are expecting (or replacep
withdiv
).我知道答案仍然很旧,我想你会有兴趣看看这个。这是我的模式。有人可能会说我滥用了 knockoutjs 的概念,但它是……它有效!
首先,您需要添加一个空模板,这是淘汰赛加载至少一个模板所必需的。如果你提供了无效的东西,knockoutjs 将无法工作并中止它正在做的事情......
假设你有这个。
现在很酷,让我们看看模板在淘汰赛中是如何工作的,淘汰赛模板实际上只是一个绑定,因此它会在运行时进行检查,并且在工作之前您可以做所有奇特的事情。这意味着你可以拥有类似的东西。
其中 activeTemplate 可以是一个可观察的对象!哦,所以可观察对象确实在那里工作......当可观察对象发生变化时,模板会收到通知,并且实际上会使用正确的模板重新渲染。
现在您了解了我们实际上可以设置模板并在将来更改它,您也了解了在加载模板之前我们将发送空模板。不会有任何绑定错误,因为没有任何东西可以绑定!
现在让我们来看看这个吧!
其中 getTemplates 定义为:
总而言之,我们正在创建一个默认加载为
false
的可观察对象。如果找到模板,我们会将值更改为 true。如果没有,我们将尝试使用 jQuery 加载它(用您喜欢的任何方式加载它...您绝对可以放置一个文本区域并从那里加载模板。一切皆有可能...)。我们返回一个计算值,该值取决于加载的可观察值。当loaded变为true时,它将返回已加载模板的id。由于某些原因,knockoutjs 要求文本位于脚本 html 标记内。这就是为什么我用 html 更新 script 标签。
就是这样。另外,我可能是错的,我不确定淘汰赛如何处理模板,但可以打开和关闭加载以重新加载模板......这意味着您可以拥有动态加载和可编辑的模板! !
另请注意,我在返回计算值之前执行了计算值。请注意,该函数是全局的......要解决这个问题!创建您自己的绑定来制作类似的东西:
然后您就可以开始了。您所要做的就是将加载逻辑放入自定义绑定中。
我将在 github 上创建一个项目,一切正常,希望它能有所帮助。稍后我会更新网址。
可能不稳定,因为我在半小时内破解了它,但它对我有用。有一天我可能不得不写一些测试,但无论如何。玩得开心!
https://github.com/llacroix/knockout-lazy-template
I know the answer is still old by I guess you'd be interested to look at this. This is my pattern. One might say that I'm abusing the concept of knockoutjs but here it is... it works!
First you need to add an empty template, this is needed for knockout to load at least a template. If you provide something invalid, knockoutjs will sadly fail to work and abort what it was doing...
Let say you have this.
Cool now lets take a look at how templating works in knockout, the knockout template is just a binding actually so it is checked at runtime and all the fancy thing you can do before works. And that mean that you can have things like that.
where activeTemplate could be an observable object! Oh so observables do works there... when the observable object changes, then the template is notified and will infact rerender with the correct template.
Now that you understand that we can actually set a template and change it in the future, you understand that until our template is loaded we will send knockout the empty template. There won't be any binding error because there is nothing to bind in nothing!
Now lets take a look at this!
Where getTemplates is defined as:
To sum it up, we are creating an observable loaded defaulted to
false
. If we find the template, we change the value to true. If not, we will try to load it with jQuery (load it with however you like... you could definitely put a text area and load the template from there. Anything is possible...).We return a computed value that depends on the
loaded
observable. When loaded becomes true, it will return the id of the loaded template. For some reasons, knockoutjs requires the text to be inside the script html tag. That's why I update the script tag with html.So that's it. Also, I might be wrong and I'm not sure how does knockout handle templates but it could be possible to switch loaded on and off to reload templates... And that means that you can have dynamically loaded and editable template awesomness!!!!
Also notice that I do execute the computed value just before returning it. And notice that the function is a global... To fix that! Create your own binding to make something like that:
And then you're good to go. All you have to do is to put the loading logic inside a custom binding.
I'll create a project on github with everything working I wish it will help. I'll update later with the url.
Might not be stable because I hacked it in half an hour but it works for me. I'll probably have to write some tests one day but anyway. Have fun!
https://github.com/llacroix/knockout-lazy-template