使用asp.net mvc时遇到一些新手敲门问题

时间:2021-06-05 16:38:23

I am new to working with knockoutjs so need some assistance around what I am trying to achieve and best practise.

我是新手使用knockoutjs所以需要一些帮助,围绕我想要实现的目标和最佳实践。

I am working on an mvc4 application where I am calling a controller action that returns json and I am then binding it to my view model eg.

我正在研究一个mvc4应用程序,我正在调用一个返回json的控制器动作,然后我将它绑定到我的视图模型,例如。

 $.getJSON("/cart/summary", function (data) {
        myModel = ko.mapping.fromJS(items);
        ko.applyBindings(myModel , document.getElementById("my-container"));
    });

The myModel view model is a direct representation of the json object returned from the controller.

myModel视图模型是从控制器返回的json对象的直接表示。

The object contains a property (Prop1) which is an object and another property (Prop2) with a list of objects.

该对象包含一个属性(Prop1),它是一个对象,另一个属性(Prop2)包含一个对象列表。

Prop1 object contains a decimal property that I would like to format as currency using the Globalize plugin. What is the best practise, should this be done in the viewmodel and binded to the view? If so, how can I extend my model to do this? Or done in the view?

Prop1对象包含一个十进制属性,我想使用Globalize插件将其格式化为货币。什么是最佳实践,如果在视图模型中完成并绑定到视图?如果是这样,我如何扩展我的模型来做到这一点?或者在视图中完成?

I want to show a div if Prop2 has more than 0 items, ie. its not empty. Similar question again, should I return a property signalling this or do it in the markup?

如果Prop2有超过0项,我想显示一个div,即。它不是空的。类似的问题,我应该返回一个信号发送此属性还是在标记中执行此操作?

I would like to bind a property to append text to a div, how is this done?

我想绑定一个属性来将文本追加到div,这是怎么做到的?

Finally, after binding is completed, I would like to animate the fact that binding is complete - dont care what the affect is, just like to know how its done?

最后,在绑定完成后,我想设想绑定完成的事实 - 不关心影响是什么,就像知道它是如何完成的?

Thanks for any feedback.

感谢您的任何反馈。

1 个解决方案

#1


1  

That's a lot of questions for one "question". I think I addressed them all.

对于一个“问题”,这是很多问题。我想我已经解决了所有问题。


If you want to use the globalize plugin, you will be best off doing the currency formatting client side. As a general rule of thumb, presentation logic should be done in the presentation layer anyway. Your business logic, and even other views may not want the currency formatting.

如果你想使用globalize插件,你最好做货币格式化客户端。作为一般经验法则,表示逻辑应该在表示层中完成。您的业​​务逻辑,甚至其他视图可能不希望货币格式化。


Again, following the same rule of thumb, the presentation layer is the thing that cares how many items your model object has. This can easily be accomplished with ko bindings.

再次,遵循相同的经验法则,表示层是关心模型对象具有多少项的事物。这可以通过ko绑定轻松完成。

http://knockoutjs.com/documentation/if-binding.html

http://knockoutjs.com/documentation/if-binding.html

<!-- ko if: listObjectName.length > 0-->
    // put your div and list bindings in here
<!-- /ko -->

To append text to a div, you can bind to the text or html of a span depending on your exact goal.

要将文本附加到div,您可以根据您的确切目标绑定到跨度的文本或html。

http://knockoutjs.com/documentation/html-binding.html

http://knockoutjs.com/documentation/html-binding.html


To put an animation after the load is complete, you can use the afterRender event.

要在加载完成后放置动画,可以使用afterRender事件。

http://knockoutjs.com/documentation/template-binding.html#note_4_using_afterrender_afteradd_and_beforeremove

http://knockoutjs.com/documentation/template-binding.html#note_4_using_afterrender_afteradd_and_beforeremove

To summarize the article, you need to set up your template:

要总结文章,您需要设置模板:

<div data-bind='template: { name: "personTemplate",
                       data: myData,
                       afterRender: myPostProcessingLogic }'> </div>

And then you can create the myPostProcessingLogic function on the myData viewmodel.

然后,您可以在myData视图模型上创建myPostProcessingLogic函数。

Here is a * post on adding a glow effect on mouse hover or at set intervals. You care most about the technique used for the interval. Instead of doing it at set intervals, you would just do it whenever the myPostProcessingLogic is called.

这是一个关于在鼠标悬停或设定的间隔添加发光效果的*帖子。您最关心的是用于间隔的技术。您可以在调用myPostProcessingLogic时执行此操作,而不是按设定的时间间隔执行此操作。

How do I animate a glowing effect on text?

如何在文本上设置发光效果的动画?

#1


1  

That's a lot of questions for one "question". I think I addressed them all.

对于一个“问题”,这是很多问题。我想我已经解决了所有问题。


If you want to use the globalize plugin, you will be best off doing the currency formatting client side. As a general rule of thumb, presentation logic should be done in the presentation layer anyway. Your business logic, and even other views may not want the currency formatting.

如果你想使用globalize插件,你最好做货币格式化客户端。作为一般经验法则,表示逻辑应该在表示层中完成。您的业​​务逻辑,甚至其他视图可能不希望货币格式化。


Again, following the same rule of thumb, the presentation layer is the thing that cares how many items your model object has. This can easily be accomplished with ko bindings.

再次,遵循相同的经验法则,表示层是关心模型对象具有多少项的事物。这可以通过ko绑定轻松完成。

http://knockoutjs.com/documentation/if-binding.html

http://knockoutjs.com/documentation/if-binding.html

<!-- ko if: listObjectName.length > 0-->
    // put your div and list bindings in here
<!-- /ko -->

To append text to a div, you can bind to the text or html of a span depending on your exact goal.

要将文本附加到div,您可以根据您的确切目标绑定到跨度的文本或html。

http://knockoutjs.com/documentation/html-binding.html

http://knockoutjs.com/documentation/html-binding.html


To put an animation after the load is complete, you can use the afterRender event.

要在加载完成后放置动画,可以使用afterRender事件。

http://knockoutjs.com/documentation/template-binding.html#note_4_using_afterrender_afteradd_and_beforeremove

http://knockoutjs.com/documentation/template-binding.html#note_4_using_afterrender_afteradd_and_beforeremove

To summarize the article, you need to set up your template:

要总结文章,您需要设置模板:

<div data-bind='template: { name: "personTemplate",
                       data: myData,
                       afterRender: myPostProcessingLogic }'> </div>

And then you can create the myPostProcessingLogic function on the myData viewmodel.

然后,您可以在myData视图模型上创建myPostProcessingLogic函数。

Here is a * post on adding a glow effect on mouse hover or at set intervals. You care most about the technique used for the interval. Instead of doing it at set intervals, you would just do it whenever the myPostProcessingLogic is called.

这是一个关于在鼠标悬停或设定的间隔添加发光效果的*帖子。您最关心的是用于间隔的技术。您可以在调用myPostProcessingLogic时执行此操作,而不是按设定的时间间隔执行此操作。

How do I animate a glowing effect on text?

如何在文本上设置发光效果的动画?