Ammar
Ammar

Reputation: 1091

Meteor Troubling Creating new Record

I'm running Meteor 0.5.2 with the insecure package on + the coffeescript package on.

Cards = new Meteor.Collection "Cards"

if Meteor.isClient

    Template.makeCard.events


        # HANDLES SUBMISSION OF NEW CARD
        'submit form.makeCardForm': ->
            makeNewCard $("input.cardName").val(), $("input.percentage").val()

  # GETS ALL THE CARDS
  Template.viewCards.cards = ->
    Cards.find {}




# METHODS


makeNewCard = (cardName, percentage) ->

    # IF NO %GE GIVEN, DEFAULT TO 0
    unless percentage
        percentage = 0

    # IF CARD NAME PRESENT

    if cardName.length
        Cards.insert
            name: cardName,
            progress: percentage

I've checked the correct values are being passed into the makeNewCard function. However, every time I submit the form, it shows up for a split second in the cards template, then disappears.

This problem does not happen when inserting records directly through the console.

Any help would be greatly appreciated.

Upvotes: 0

Views: 113

Answers (1)

mu is too short
mu is too short

Reputation: 434635

I'm not that familiar with Meteor but I think I see your problem. You have a submit handler on you a form:

'submit form.makeCardForm': ->
    makeNewCard $("input.cardName").val(), $("input.percentage").val()

That handler isn't returning false so the form submission will continue just like a normal <form> submission; so you see the results you're expecting for a split second and then the <form> submits to the server as usual and everything disappears.

Compare this (http://jsfiddle.net/ambiguous/Q6cQr/):

<form>
    <input type="submit" value="submit">
</form>​

$('form').on('submit', ->
    console.log('Doing things!')
)​​

and this (http://jsfiddle.net/ambiguous/XWkXG/):

<form>
    <input type="submit" value="submit">
</form>​

$('form').on('submit', ->
    console.log('Doing things!')
    false
)​​

and you should see the difference.

You probably just need to add a return false to your submit handler:

'submit form.makeCardForm': ->
    makeNewCard $("input.cardName").val(), $("input.percentage").val()
    false

Upvotes: 1

Related Questions