chrismccord/sync — pull request #114 by wursttheke (2014-04-07 23:35:27+00:00)
Title: Automatic Sorting

Ok, here's my go at this: #112 (not yet complete)

In order to get client side sorting to work, we need to pass the necessary information down to the client JS. This approach uses some additional data-* tags to transport that info and stick it to the specific collection items.

I figured this feature (and approach) only makes sense in conjunction with the usage of sync scopes, because only then we know about the necessary order information after the initial page load.

I also figured that the separate `sync_new` call now loses its sense somehow. So I ditched it and instead added a surrounding 'container-script-tag' around the collection, which includes the PartialCreator code automatically:

```ruby
class User < ActiveRecord::Base
sync :all
sync_scope :with_highscore, -> { order(highscore: :desc).limit(10) }
end
```
```erb
<%= sync :partial "user", collection: User.with_highscore %>
```
The new resource instance is automatically instantiated internally by calling `new` on the collection (`User.with_highscore`) if possible. If you do not wish to sync new records, you can pass `new: false` as an option. It also checks the collection for existing order statements, extracts them and attaches the direction (asc, desc) to the container script tag and the values (e.g. highscores) to the one of each item. It also sets the `:direction` option to `:sort`. If no order info is found on the collection, the direction will be set to `:append`. You can overwrite that by passing `direction: :prepend` if you wish.

The following HTML will be generated:

```html

Chris (Higscore: 1396601442)

Philipp (Higscore: 3452345)

```

Now, the whole collection is nicely delimited and can be handled with jQuery. The `PartialCreator` JS is not sitting around somewhere in the DOM, but in the 'head' of the collection it belongs to.

Please take a look at the code. It's not complete yet. The whole JS-magic is still to come.

Let me know what you think.

Philipp