问题
How do I go about sorting a set of entities in a breeze entity? For example if I have an order with many items, how do I sort the items by orderDate?
<div databind="ko with: order">
<ol databind="foreach: items">
<li/>
</ol>
</div>
Also, I can't use items.sort() in the data-bind, because I am making the list items sortable. https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDYQFjAA&url=https%3A%2F%2Fgithub.com%2Frniemeyer%2Fknockout-sortable&ei=8BJ3UaXZMMGG2wW5yoGQCw&usg=AFQjCNFnjZ_6ths9dl_UW9BpJkFO3Yi6kA&sig2=swWZW7Rq6IqAcIX2-2Cl6w&bvm=bv.45580626,d.b2Istrong text
回答1:
I think the short answer is "you can't". The Breeze entity properties are simple, unordered ObservableArrays because they are part of the model.
What you want to achieve is a UI effect ... which, strictly speaking, does not belong in the model. The model is supposed to be UI independent. If the Order data are stored in a SQL database, the sequence of the Order's items is strictly undefined there.
Enough theory. You have work to do. What might I do? One of two things
1) Use an OrderViewModel
"OrderViewModel" is an "ItemViewModel" which is a wrapper around a model object that has "special powers" in support of a view. The OrderViewModel can expose its wrapped Order object for binding most of its properties. But when you need something special ... such as your sorted "items" ... you create a KO property on the OrderViewModel for display purposes ... call it "sortableItems" and bind to THAT in your view.
Here's a simplistic, pseudo-code implementation:
app.OrderViewModel = function(order) { this.order = order; this.sortableItems = ko.observableArray(); this.resetSortableItems(); } app.OrderViewModel.prototype.resetSortableItems() { // (re)initialize with copy of the items this.sortableItems(this.order.items().slice(0)); // maybe some ordering logic here? }
Your "sortableItems" covers the inner Order.items
in the manner that you intended; it would be sortable, perhaps by means of the KO-Sortable feature to which you referred.
The hard part (not shown) is keeping it in sync with the inner Order.items
property when items are added or removed. Remember the inner Order.items
property is the real source of truth. Maybe you're luck and in complete command of whatever can add/remove items; then you can just call the resetSortableItems
method when those changes occur.
This is as far as I can take you.
2) Extend the Order entity with Order.sortableItems
Maybe you've decided not to be such a purist. You're willing to modify the model entity to make it play well with your UI. So you add that KO-Sortable property that I just described ... but add it to the Order
entity in a Custom Initializer rather than to an OrderViewModel.
This may simplify things for you. On the other hand, you've (a) polluted the Order entity with UI concerns and (b) limited yourself to a single sort of the items which would apply to all views that reference a given Order. To (a) you may say "who cares?" (I do it all the time). But (b) may be a problem ... in which case you go back to the OrderViewModel for maximum flexibility.
来源:https://stackoverflow.com/questions/16180592/sorting-breeze-navigation-properties