Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asteroid & Subscriptions #116

Open
aadamsx opened this issue Mar 13, 2017 · 5 comments
Open

Asteroid & Subscriptions #116

aadamsx opened this issue Mar 13, 2017 · 5 comments

Comments

@aadamsx
Copy link

aadamsx commented Mar 13, 2017

Before switching to React or Vue, I'm trying out Blaze with Asteroid (since my existing code base is Blaze). Most all examples I see are Asteroid + React/Redux, where the contents of the Subscription are piped into the Redux store.

The only issue I see so far with Asteroid is, when using it with Blaze (as a test), I don't see a way to pipe in the subscription into mini-mongo.

For example, I inside the ddp.on('added' ...) I cannot do a People.findOne({userId: ID}), like I could with mini-mongo. So at least with Blaze, there seems to be no way to get the data out from the subscription...

// asteroid.js
asteriod.subscrib('People');

asteroid.ddp.on('added', (doc) => {
  if (doc.collection === 'People') {
    // I see the data
    // wish I could query like let ppl = People.findOne({userId: ID});
  }
}); 

// names.js
Template.namesOverview.onCreated(function() {
   // let ppl = People.find({}); // doesn't work!
});

I get an Object inside 'added' like so:

collection: "People"
fields: Object
  createdAt: Object
  firstName: 'Test'
  lastName: 'Test'
id: "asdfWkfasedf"
msg: "added"

My question, is there a way to query the subscription like I do in mini-mongo?

@davidebianchi
Copy link
Contributor

davidebianchi commented Mar 16, 2017

Hi @aadamsx, I'm not sure to have understood. To make query to mongo, you should have a specific publication on meteor server. The added event on asteroid return an object with the keys collection, fields and id and include the element added to collection and not the entire collection.

You should save the collections somewhere in your application as plain JS object.

If you want to save in another structure, you can make your own mixin (like the asteroid-immutable-collections-mixin which convert the collections in Immutable.js).

@aadamsx
Copy link
Author

aadamsx commented Mar 16, 2017

To make query to mongo, you should have a specific publication on meteor server

Yes, done that

The added event on asteroid return an object with the keys collection, fields and id and include the element added to collection and not the entire collection.

Yes, I've seen that, I get the following object for example:

collection: "People"
fields: Object
  createdAt: Object
  firstName: 'Test'
  lastName: 'Test'
id: "asdfWkfasedf"
msg: "added"

You should save the collections somewhere in your application as plain JS object.

OK, this is the part were I fell down. So save the object in a Session variable for example?

If you want to save in another structure, you can make your own mixin (like the asteroid-immutable-collections-mixin which convert the collections in Immutable.js).

Thank you, I'll check this out.

@davidebianchi
Copy link
Contributor

OK, this is the part were I fell down. So save the object in a Session variable for example?

Yes, something like this. For example in React application we use the redux store to save the collections.

@aadamsx
Copy link
Author

aadamsx commented Mar 18, 2017

For example in React application we use the redux store to save the collections.

Right, and in Blaze we use mini-mongo. So I played with this more, and it's going to be difficult doing anything with that return object, in part I guess because I don't have a redux store.

What would be great is if I could do something like what we can do with a straight DDP connection, for example. This makes it so easy to get Collections in the proper format from the remote server!

http://stackoverflow.com/a/18360441/2547360:

var remote = DDP.connect('http://server1.com/');
Items = new Meteor.Collection('items', remote); 

remote.subscribe('items', function() {
  var items = Items.find();
  console.log(items.count());  // get 1         
});

Notice how I can take the connection and pass it to the Meteor.Collection, and from there query it acts like mini-mongo on the client (with a .find())? Is there a way to do this in Asteroid?

It seems there use to be some equivalent to this before with Asteroid:

https://www.npmjs.com/package/asteroid.browser

// Connect to a Meteor backend 
var ceres = new Asteroid("localhost:3000");
 
// Use real-time collections 
ceres.subscribe("tasksPublication");
var tasks = ceres.getCollection("tasks");
tasks.insert({
  description: "Do the laundry"
});

But I can't find .getCollection()

@patrickcneuhaus
Copy link

Hello guys!

I have the exact same issue as @aadamsx . Were you able to solve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants