Two ways after pushing the button

Hi there!
I have a question. Is it possile that my button could have two ways after I submit that?

For example I want to add a new product. I choose the “Create product” site in my menu and I could create that product by filling the table on that site. After I fill that I push “Save” button and I have an answer above the table: “Saved.” and nothing else happens.
On the other hand I have something like an invoice. I could create a new product from the invoice site. I click “add new product” on my invoice site and fill in the table in the same way. After I click “Save” I don’t want to get the “Saved.” message, but get back to my invoice and get the product checked on my list.
Is it possible?

The question is rather related to programming in general then to Meteor.

Answering your question: yes, it’s possible:

Template.saveButton.events({
  'click': function() {
    if ( /* isInvoice */ ) {
      // process; go back to invoice; check product
    } else {
      // print "Save"
    }
  }
});

Yeah, thanks. But is it possible to take something from an invoice that the “click function” could know, that “I now handle the invoice” ?

It’s up to you to decide how to trigger this : )

For example you can do:

var isInvoice = false;

Template.createProductButton.events({
  'click': function() {
    isInvoice = true;
  }
});

Template.addNewProduct.events({
  'click': function() {
    isInvoice = false;
  }
});

Template.saveButton.events({
  'click': function() {
    if (isInvoice) {
      // process; go back to invoice; check product
    } else {
      // print "Save"
    }
  }
});

But there are many ways and possibilities. Maybe you can rely on current url state. Or you have some variable in Session, that can track, that you are now handling the invoice. Or anything else which suits best for this situation and your code architecture.

I need to admit that I use session, because I want to get the full info of my product under the dropdown (selection) in my invoice when I choose specific product :slight_smile: I’ll try with what You’ve done for me and let you know after :smile: Thanks :slight_smile:

well, you can start validation and as a callback prepare 2 states - in case of issues write error message.
And if all went well call method to update database and as a callback you can get back new product ID, so u can for example call Router.go(’/products/’+this._id) to redirect to that product or similar call for flow router etc.

As an aside, you can use Blaze.getData(event.currentTarget) to get the full data from a dropdown and you won’t need sessions.

Hmmm… Are you sure?
In my Template.xxx.events I do:

"change select": function(e,t){
            Session.set("selectedCustomer",e.target.value);
            var proba = Session.get("selectedCustomer");
    },

May I do sth like:

"change select": function(e,t){
            var proba = Blaze.getData(e.currentTarget) 
    },

I need it later, cause in my Template.xxx.helpers I do:

walutaklienta:function(){
        var klient = Customers.findOne({ _id: Session.get('selectedCustomer')});
        ...
        here some code
         ...
return xxx
},

I just need to get from the Customer to the Currency Name by the ID. It’s a long helper and just not acceptable to read it clearly, so I just cut that off.
How can I do it with a Blaze getData? When I do:

var lipton = Blaze.getData(e.currentTarget);
var pracownik = Customers.findOne({_id: liptonik._id});
console.log(pracownik);

I just get the Array Object. But the Id of my Customer is in LocalCursor, so it’s bad, cause my customer is not fit to the invoice by which ID I would find the info about my Customer. It’s not defined, cause my invoice was not saved. Probably ofc.

Ok. I made a variable and it works :smile:
Noooowwww… is the question.
I want to take the info from my product to the invoice.

In steps:

  1. Create new invoice
  2. Push button “New Product”
  3. Go to “Create product” page and create a new product
  4. Click “Save” and go back to new invoice
  5. The checked product from dropdown should be my “new product” and info of that product must be under dropdown.

It’s something like in “Create Product” I create and ID of a product and I want it to be checked after I go back to my new invoice. Now, when I go back to my new invoice, every data is refreshed and has default value.

OK. So I did steps 1 to 4. I don’t know how to take the ID variable to be checked in my new invoice. Do I need to use session or sth?