jmorrell / backbone.obscura Goto Github PK
View Code? Open in Web Editor NEWA read-only proxy of a Backbone.Collection that can be filtered, sorted, and paginated.
Home Page: http://jmorrell.github.io/backbone.obscura/
License: MIT License
A read-only proxy of a Backbone.Collection that can be filtered, sorted, and paginated.
Home Page: http://jmorrell.github.io/backbone.obscura/
License: MIT License
When calling:
proxy.models
There is "undefined" returned.
So, in case of rendering a collection from a proxy that requires models instead of JSON, having models would make the proxy more complete.
Hey,
i just came across the task of grouping models. Would that make sense to use a proxy for that?
i implement that by creating a new grouped collection and wrapping the collection in a new proxy. but i loose the referenz to the original proxy and can't "reset" the original state.
kalle
Hi there,
Currently, when I add a filter to my obscura collection, the filtered models are being rendered into views one by one, and not by the CompositeView as a collective whole.
I need an event hook for when all the views have finished rendering, and because itemViews are being rendered individually, I don't have a way of knowing when that is.
Collection/Composite views have various onRender events that I could use, except they're not being triggered.
I have a collection of models that looks like this:
[
{
"id": 1,
"name": "mike",
"pets": [
{ "id": 1, "type": "dog" },
{ "id": 2, "type": "cat" }
]
},
{
"id": 2,
"name": "john",
"pets": [
{ "id": 1, "type": "dog" }
]
}
]
is it possible with obscura to filter the users that have, for example, a cat?
backbone.obscura kick ass when the collection has everything.
But suppose i'm receiving some pagination from an API. My collection has "right now" 50 models, but there are hundreds more on the API.
Did you ever tackle this generic problem with your library or it wasn't designed with this in mind ?
I'm asking because i'm not seeing anything like that in the docs and i'm not seeing some obvious way I could plug in to fetch incrementally from the server when going next/previous page.
Currently there is no way of knowing how many models are in the collection after filtering if you're also paginating. This is definitely information you may want to show in the UI.
It would be nice to have some filter by Array function:
movies.filterBy('ratings', [3,4,5]);
Then, all movies that have a rating of 3 OR 4 OR 5, should be returned. It looks a bit familar to
filtered.filterBy('foo and bar filter', { foo: 2, bar: 3 })
but with an Array.
What do you think? I can try a PR for this.
Cheers.
Should probably be .setFilter
to match the other method names: https://github.com/jmorrell/backbone.obscura#api
The filtered:add
event is triggered in addFilter method, but addFilter
method is executed before execFilter
.
filterBy: function (filterName, filter) {
if (!filter) {
filter = filterName;
filterName = this.defaultFilterName;
}
addFilter.call(this, filterName, createFilter(filter));
execFilter.call(this);
return this;
},
I think that filtered:add
should be triggered when the collection is already filtered.
I'm sure this is by design but I'm a bit confused as to why. For this to really work in all cases the object itself should be instanceof Backbone.Collection. There are some libraries (Backbone.Epoxy for example) that accept collections and check if they are an instance of Backbone.Collection. If they are not, they do not work.
Hi there,
I've run into a situation where what I need is essentially an instance of Obscura, but one that merges multiple collections together. The specific use case is a combined list of "things" being fed into a Marionette CollectionView and collectively sorted by date.
I have a (currently broken) proof of concept at https://gist.github.com/mcordingley/7900358 based somewhat on some of your code.
It would be awesome if this sort of thing could make it into Obscura.
Thanks,
-Michael
Lets say we have a collection sorted on attribute 'name', and there are 4 entries with the name "Bob", if we change some other attribute, say 'age' on the second "Bob", it will remove that one and insert it at the beginning or the end of the "Bob"s, depending on directionality.
Cause:
If we consider line https://github.com/jmorrell/backbone.obscura/blob/master/backbone.obscura.js#L641, the algorithm checks to see where it would be inserted, and if its current index is different from the new index. However this doesn't take into account whether the comparator of the model at the new index is different from the comparator of the model in question. If they are the same, they shouldn't be resorted.
This can cause very disruptive changes, especially when there's a small number of unique comparators compared to the size of the list.
As pointed out in this comment: jmorrell/backbone-paginated-collection@86ef135#commitcomment-4475558
Calling .sort() on the original function should update the proxy.
I'm using backbone obscura in my application.
In my require_main.js inside paths I've
'backbone.obscura':'lib/backbone.obscura',
and in shim I've
'backbone.obscura' : {
deps : [ 'underscore', 'backbone' ],
exports : "Obscura"
}
I'm pulling in obscura in one of my Marionette view
define(['backbone.obscura'], function(Obscura){
// code for view creation. Edited for brevity
showTable : function(){
console.log(this.collection.length); // prints the length of collection which is 10
var proxy = new Obscura(this.collection);
console.log(proxy.length); // prints the length of proxy which is one (1) // remaining 9 are not retrieved
var view = new Collectionview({collection : proxy});
}
});
Could you please suggest any approach to figure out this issue?
I currently have a 'change' event on my model which works fine until I do any filtering.
in my collection entity I have:
initialize: ->
@on("change:selected", @toggleSelected)
toggleSelected: (selectedModel, isSelected) ->
if isSelected
_.each @models, ((model) ->
model.deselect() if selectedModel isnt model
)
and this is how I've initialized my collection
contacts = new Entities.ContactsCollection
contacts.fetch
reset: true
@contacts = new Backbone.Obscura(contacts)
@contacts
Is there a reason why the model 'change' event is not working within obscura?
Not sure if this is a bug, or if I need to run the sort as a fuction and parse the stored string as an int.
Check out the SS:
https://www.dropbox.com/s/0sedm8cuedtj29e/Screenshot%202014-01-25%2000.13.59.png
It goes:
11, 110, 22
What ya think?
As the title suggest - do you have any thoughts on how Obscura might be extended to allow for those two use cases:
Thanks!
@jmorrell Great work on this library!
One thing I'd like to do is perform sorts using a compareFunction comparator callback similar to how Array.prototype.sort does it. The callback provides two models as arguments (a and b), and the result of the function is whether A is greater than B, vice-vera, or they're equal. This allows you to do much more complex sorts that are multi-layered and don't have to rely on string comparison.
The following issue calls out this issue as it originally appeared in Backbone. The ability to provide a compareFunction-like sort comparator has since been added.
Would this be difficult to add? I might look into doing it if I get some time soon.
Related to jmorrell/backbone-collection-proxy#2
I've missed this when i opened the previous issue, but Obscura took its events from an internally created collection and not from its superset.
I'd happily open a PR for this, but for now i'm not sure about how to tackle this cleanly.
I'm not actually sure if there's something I'm doing wrong, but for all my Obscura collections, when a change event is fired on the superset collection (e.g. a model's properties have been changed), the model being changed moves to the bottom of the collection.
Backbone.Obscura calls the following code:
function onChange(model) {
if (this.contains(model)) {
this._collection.remove(model);
onAdd.call(this, model);
}
}
which means that the model is removed, then re-added to the list. Though it looks like it's index is being retained in the onAdd method, so I'm not sure what is going on.
If a model's property changes, does the collection need to remove/add it? Shouldn't the model already have the change?
Any thoughts?
Based on a quick test, it looks like the only conflict with the new version of backbone is the removal of Collection#sortedIndex. This should be pretty easy to remedy.
Minor noise during bower install
bower backbone.obscura#~1.0.0 mismatch Version declared in the json (0.1.7) is different than the resolved one (1.0.0)
Too late to fix it for 1.0.0 since that's already tagged+published but maybe something to add to a release checklist (mental or otherwise) for future releases.
I am unable to sort alphanumeric strings by using sortBy function. Could you look in to that one.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.