[Package] Grapher + Vue Helper


#1

I’m working on a Vue Mixin to manage Grapher queries.
It automatically subscribes to the queries when the Vue instance is created, and unsubscribes when it’s destroyed, and it updates them when Vue variables they depend on change.

You can use it like this:

<template>
	<ul>
		<li v-for="user in users.data">{{user.username}}</li>
	</ul>
</template>

<script>
export default {
	data(){
		return {
			limit:20,
		}
	},
	grapher:{
		users(){
			return {
				collection:Meteor.users,
				query:{
					username:1,
					$options:{limit:this.limit}
				}
			}
		}
	}
}
</script>

The package is now available on Atmosphere and GitHub!

Demo

(not guaranteed to be up)

You can also clone my testing repository from GitHub, it’s a Meteor project with some test data and everything set up.

Any suggestions would be appreciated :slight_smile:

It doesn’t support Meteor reactivity for updating query options at the moment, only Vue reactivity.

@robfallows @diaconutheodor


#2

That sounds good, you can get some inspiration to how I treated it in https://atmospherejs.com/cultofcoders/grapher-react

If a query is reactive then i’m wrapping it in a meteor tracker, otherwise I’m using React’s way of doing things.


#3

Definitely interested in this :thumbsup: - but I’ve not used Vue before, so at the moment it will be more of an academic exercise for me. :slight_smile:


#4

Okay, published the package now!


#5

@robfallows Did you check out my demo? :slight_smile:
I think it’s pretty neat how little code was necessary to create it, even with all the features it has.


#6

Yes I did :slight_smile: - I think I lost a large part of my day being mesmerised by the transitions :wink:.

One slight oddity I don’t understand - why does the entire stylesheet get rendered at the start of the page?


#7

Ah, I was just messing around with CSS and tested if you could make <head> and its children visible :stuck_out_tongue:
The latest version doesn’t have that.


#8

I’ve updated my testing repo with a pretty cool infinite scroll feature, combining grapher-vue with Akryum’s vue-virtual-scroller!

It smoothly scrolls through a list of 50.000 items that are fetched on-demand from the server, only subscribing to those that are visible based on scroll position

For lazy people, the demo may or may not be available at https://dev.herte.by/ depending on what I’m doing at the moment :stuck_out_tongue:


#9

This demo is very nice! :+1: Awesome!


#10

That is really sweet! :slight_smile:


#11

Yeah, @herteby you should probably publish a cordova version of that colored boxes section on the app store so I can take it everywhere I go, you know, to just pointlessly yet soothingly stare at it for copious amounts of time. :open_mouth: :smiley:


#12

Hi, i got an error when use with vue-ssr.

W20171009-18:35:20.687(7)? (STDERR) TypeError: query.subscribe is not a function
W20171009-18:35:20.687(7)? (STDERR)     at run (packages/herteby:grapher-vue/grapher-vue.js:88:17)
W20171009-18:35:20.688(7)? (STDERR)     at [object Object]._onTimeout (packages/herteby:grapher-vue/grapher-vue.js:148:10)
W20171009-18:35:20.689(7)? (STDERR)     at Timer.listOnTimeout (timers.js:92:15)


#13

@nxcong I’ve replied to your github issue.


#14

Quick tip, you can also avoid specifying collection, if u use the collection’s name:

createQuery({
    users: { ... }
})

If you want to avoid having both collection and query params specified. But I agree that this is cleaner version. The only reasoned I added the functionality above was easy testing with Grapher Live.

Usually I like keeping queries modularised in their own file. So having something like: users: getUsersQuery should also be an option. Because in time queries grow a lot in their vertical size, and you keep the code cleaner this way.


#15

“collection” and “query” are not the only arguments though, there are 5 other optional ones too.

If you put the query in another file, you couldn’t use the reactive Vue component properties in it though. I guess you could export a function that returns the query, which you then call with function.call(this) :thinking:


#16

This is why I created the .clone() function of a query. query.clone() creates a new instance of it.


#17

That still leaves you with the problem of being outside Vue component scope and not having access to the component data though.

Hmm, I guess I didn’t think of setParams and $filter when I made the package. That would be another way to do it, and would work better with named queries too :thinking: I guess I’m not quite following the intended use.


#18

There must be a way to reuse modules inside a vue module. Either with import, either by defining a global namespace like:
App.Queries.getAllUsers

If vue does not allow this, or the integration of vue in meteor, then something is really bad with it. It should be modular.


#19

Yes, you can can use import/export with Vue, and for a static query this would be fine.

How grapher-vue works though is that it takes the function you define and puts it in a Vue watcher, which reruns the function and updates the query whenever any reactive Vue variables (this.limit in the example) that you’ve used in it change. So if you have pagination or search for example, it automatically updates the query when you change the “search” or “page” variables on the Vue component. This requires that grapher-vue is given a function rather than an object.

I realize now that your intention was probably to use setParams and $filter for this type of functionality, though that way is still slightly less flexible than the way I’ve done it.

I should probably add a params argument though, so that you can use grapher-vue with named queries + dynamic params, which isn’t possible at the moment.


#20

Any updates on you @herteby adding the $filter and setParams options to grapher-vue?