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

How can i use hooks on a Collection2 insance? #50

Closed
kevohagan opened this issue Dec 12, 2013 · 7 comments
Closed

How can i use hooks on a Collection2 insance? #50

kevohagan opened this issue Dec 12, 2013 · 7 comments

Comments

@kevohagan
Copy link

Or from a collection2 how can i define a autoform instance to use it's hooks system?
i looked at the autoform example but i dont get how it defines the documents.hooks since its a collectin2 instance and shouldnt be able to use the callbacks/hooks.

Thanks a lot 👍

@aldeed
Copy link
Collaborator

aldeed commented Dec 13, 2013

Just create an AutoForm instance on the client, passing in the Collection2 as the only argument to the constructor. Then attach all your hooks to the AutoForm and use the AutoForm for your autoForm helper's schema attribute.

On Thu, Dec 12, 2013 at 3:59 AM, kevohagan notifications@github.com
wrote:

Or from a collection2 how can i define a autoform instance to use it's hooks system?
i looked at the autoform example but i dont get how it defines the documents.hooks since its a collectin2 instance and shouldnt be able to use the callbacks/hooks.

Thanks a lot 👍

Reply to this email directly or view it on GitHub:
#50

@kevohagan
Copy link
Author

It's strange because in your example you dont pass the Collection2 Documents in an instance of AutoForm to get your hooks.

I define a collection2 Workshops

That is what im doing on the client :

 {{#autoForm schema=WorkshopsForm id="insertWorkshopForm"}}

with

 WorkshopsForm = new AutoForm(Workshops);

         WorkshopsForm.hooks({
            insert: function(error, result) {
                if (error) {
                    console.log("Insert Error:", error);
                } else {
                    alert("Inserted!");
                    console.log("Insert Result:", result);
                }
            },
            update: function(error) {
                if (error) {
                    console.log("Update Error:", error);
                } else {
                    alert("Updated!");
                }
            },
            remove: function(error) {
                console.log("Remove Error:", error);
            }
        });

and i get Exception from Deps recompute: TypeError: Cannot call method 'simpleSchema' of undefined

im definitely missing something simple

@kevohagan
Copy link
Author

If i define it like

 {{#autoForm schema=Workshops id="insertWorkshopForm"}}

with



         Workshops.hooks({
            insert: function(error, result) {
                if (error) {
                    console.log("Insert Error:", error);
                } else {
                    alert("Inserted!");
                    console.log("Insert Result:", result);
                }
            },
            update: function(error) {
                if (error) {
                    console.log("Update Error:", error);
                } else {
                    alert("Updated!");
                }
            },
            remove: function(error) {
                console.log("Remove Error:", error);
            }
        });

i get Exception from Deps afterFlush function: TypeError: Object [object Object] has no method 'hooks'

but if i call it with Workshops.callbacks it works!

but console returns

myCollection2.callbacks is deprecated; use myCollection2.hooks 

weird

@aldeed
Copy link
Collaborator

aldeed commented Dec 14, 2013

Maybe a bug. I won't be around a computer for a couple days, but I'll test as soon as I can.

On Fri, Dec 13, 2013 at 11:44 AM, kevohagan notifications@github.com
wrote:

If i define it like

 {{#autoForm schema=Workshops id="insertWorkshopForm"}}

with

         Workshops.hooks({
            insert: function(error, result) {
                if (error) {
                    console.log("Insert Error:", error);
                } else {
                    alert("Inserted!");
                    console.log("Insert Result:", result);
                }
            },
            update: function(error) {
                if (error) {
                    console.log("Update Error:", error);
                } else {
                    alert("Updated!");
                }
            },
            remove: function(error) {
                console.log("Remove Error:", error);
            }
        });

i get Exception from Deps afterFlush function: TypeError: Object [object Object] has no method 'hooks'
but if i call it with Workshops.callbacks it works!
but console returns

myCollection2.callbacks is deprecated; use myCollection2.hooks 

weird

Reply to this email directly or view it on GitHub:
#50 (comment)

@chrisbutler
Copy link
Contributor

@kevohagan i was stumped by the same issue for a little while. your original example was 99% there, you just forgot quotes around schema='WorkshopsForm'

@aldeed
Copy link
Collaborator

aldeed commented Dec 17, 2013

Did you resolve this? Let me clarify a few things:

  • You can pass either a Collection2 instance or an AutoForm instance as the schema. BUT if you want to add hooks, you need to use an AutoForm instance (wrapping the C2) because you can't add the hooks to a Collection2.
  • Even if you don't need hooks, you'll get a little better performance by using AutoForm wrapper instead of passing the C2 directly. Being able to pass the C2 directly is for backwards compatibility and simplicity, but isn't the recommended way. (Docs should probably be updated a bit.)
  • As far as how to pass the C2/AF instance to the helper, you can do so either as a string or as another helper. If you use a string, it looks in the window scope for an object with that name. Otherwise by using a helper you are directly binding. Again, using the helper is the recommended way because you'll get better performance and you then could use your own namespace, but passing the string name is easier to understand and less code.

In the specific issue above, as @chrisbutler mentioned, you can either add quotes around WorkshopsForm or you can make a WorkshopsForm helper that returns the WorkshopsForm object, like this:

Template.myTemplateName.WorkshopsForm = function () {
    return WorkshopsForm;
};

@kevohagan
Copy link
Author

Yes Perfect resolved it!

Much clearer now ;) . For the record that is what i did :


Template.new_workshop.newWorkshopsForm = function () {


 newWorkshopsForm = new AutoForm(Workshops);

         newWorkshopsForm.callbacks({
            insert: function(error, result) {
                if (error) {
                    console.log("Insert Error:", error);
                } else {
                    alert("Inserted!");
                    console.log("Insert Result:", result);
                }
            },
            update: function(error) {
                if (error) {
                    console.log("Update Error:", error);
                } else {
                    alert("Updated!");
                }
            },
            remove: function(error) {
                console.log("Remove Error:", error);
            }
        });

    return newWorkshopsForm;
};

works like a charm! :)

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