It will be sooooo long. Forgive me. I’ll try to cut it as much as I can
Invoices.new.new.html:
<template name="InvoicesNewNowaFaktura">
<div class="form-group field-customer-id" id="inputcustomer">
<label for="customerId" class="boldy">
TO
</label>
<div class="input-div">
<select id="ThemeSelect" class="form-control" name="customerId" required="required">
<option value="" disabled >Choose your customer...</option>
{{#each customers}}
{{#if selectedCustomer newId}}
<option value="{{_id}}" selected>
{{name}}
</option>
{{else}}
<option value="{{_id}}">
{{name}}
</option>
{{/if}}
{{/each}}
</select>
</div>
<div>
<p>
{{ klient.name}}
</p>
<p>
{{ klient.business_address}}
</p>
<p>
{{ klient.email }}
</p>
</div>
<button id="customernewbutton" type="button">
Create new customer
</button>
</div>
</div>
</template>]
Invoices.new.new.js
Template.InvoicesNewNowaFaktura.events({
"submit": function(e, t) {
e.preventDefault();
pageSession.set("invoicesNewNowaFakturaInfoMessage", "");
pageSession.set("invoicesNewNowaFakturaErrorMessage", "");
var self = this;
function submitAction(msg) {
var invoicesNewNowaFakturaMode = "insert";
if(!t.find("#form-cancel-button")) {
switch(invoicesNewNowaFakturaMode) {
case "insert": {
$(e.target)[0].reset();
}; break;
case "update": {
var message = msg || "Saved.";
pageSession.set("invoicesNewNowaFakturaInfoMessage", message);
}; break;
}
}
//Router.go("invoices", {invoiceId: this._id});
Router.go("invoices.details", {invoiceId: t.data.fakturka._id});
Session.set('selectedCustomer', undefined);
delete Session.keys.selectedCustomer;
}
function errorAction(msg) {
var message = msg || "Error.";
pageSession.set("invoicesNewNowaFakturaErrorMessage", message);
}
validateForm(
$(e.target),
function(fieldName, fieldValue) {
},
function(msg) {
},
function(values) {
Invoices.update({ _id: t.data.fakturka._id }, { $set: values}, function(e) { if(e) errorAction(e.message); else submitAction();});
}
);
Session.set("selectedCustomer",undefined);
return false;
},
"change select": function(e,t){
console.log(e);
Session.set("selectedCustomer",e.target.value);
var proba = Session.get("selectedCustomer");
console.log(proba);
console.log(e);
},
"click #customernewbutton": function(e, t){
isInvoice = 2;
console.log(isInvoice);
Session.set("selectedCustomer",e.target.value);
idOfInvoice = t.data.fakturka._id;
Router.go("customers.insert", {});
},
Template.InvoicesNewNowaFaktura.helpers({
klient:function(){
return Customers.findOne({ _id: Session.get('selectedCustomer')});
},
walutaklienta:function(){
var klient = Customers.findOne({ _id: Session.get('selectedCustomer')});
var walutaid = klient.currency;
var walutaogolnie = Currency.findOne(walutaid);
var walutaname = walutaogolnie.name;
return walutaname;
},
*** Your type of code ;) ***
selectedCustomer:function(newId){
console.log(newId);
console.log(this);
return this._id == Session.get('selectedCustomer');
},
'nextinvoiceNumber':function(){var max=0; var invoiceNumbers=Invoices.find({},{fields:{invoiceNumber:1}}).fetch();_.each(invoiceNumbers,function(doc){var intNum=parseInt(doc.invoiceNumber);if(!isNaN(intNum)&&intNum>max)max=intNum;});return max+1;}
});
Customers.insert.insert.html
<template name="CustomersInsertInsertForm">
<div class="form-group saveblock">
<div class="submit-div">
<button id="form-submit-button" class="btn btn-success" type="submit">
<span class="fa fa-check">
</span>
Save
</button>
<a href="#" id="form-cancel-button" class="btn btn-default">
Cancel
</a>
</div>
</div>
</template>
Customers.insert.insert.js
Template.CustomersInsertInsertForm.events({
"submit": function(e, t) {
e.preventDefault();
pageSession.set("customersInsertInsertFormInfoMessage", "");
pageSession.set("customersInsertInsertFormErrorMessage", "");
var self = this;
function submitAction(msg) {
var customersInsertInsertFormMode = "insert";
if(!t.find("#form-cancel-button")) {
switch(customersInsertInsertFormMode) {
case "insert": {
$(e.target)[0].reset();
}; break;
case "update": {
var message = msg || "Saved.";
pageSession.set("customersInsertInsertFormInfoMessage", message);
}; break;
}
}
if(isInvoice == 2){
Router.go("invoices.new", {invoiceId: idOfInvoice});
}
}
function errorAction(msg) {
var message = msg || "Error.";
pageSession.set("customersInsertInsertFormErrorMessage", message);
}
validateForm(
$(e.target),
function(fieldName, fieldValue) {
},
function(msg) {
},
function(values) {
newId = Customers.insert(values, function(e) { if(e) errorAction(e.message); else submitAction(); });
if(isInvoice == 2)
{
console.log(newId); ***<-- it gives me an ID of my new customer***
console.log(idOfInvoice); ***<-- id gives me and ID of my Invoice***
Invoices.update({ _id: idOfInvoice }, {$set: {customerId:newId}});
}
}
);
return false;
},