Knockoutjs list in a list select binding - list

I have seen so many example of this problem but not as a list. Here's my code:
<div>
<form>
<p>You have asked for <span data-bind='text: gifts().length'> </span> gift(s)</p>
<input id='giftname' type='text'/><button data-bind='click: createGift'>Add Gift</button>
<table>
<thead>
<tr>
<th>Gift name</th>
<th>Pack</th>
<th>Price</th>
<th />
</tr>
</thead>
<tbody data-bind='foreach: gifts'>
<tr>
<td><input data-bind='value: name' readonly='readonly'/></td>
<td><select data-bind="options: packs,
optionsText: 'pack',
value: price" /></td>
<td><input data-bind="value: price ? price.packprice : 'unknown'" readonly="readonly"/></td>
<td><a href='#' data-bind='click: $root.removeGift'>Delete</a></td>
</tr>
</tbody>
</table>
<button data-bind='enable: gifts().length > 0' type='submit'>Submit</button>
</form>
</div>
<script type="text/javascript">
var GiftModel = function(gifts) {
var self = this;
self.gifts = ko.observableArray(gifts);
self.addGift = function(gift) {
var newGift = {
name: gift.name,
packs: gift.packs,
price: gift.price
};
self.gifts.push(newGift);
};
self.removeGift = function(gift) {
self.gifts.remove(gift);
};
self.createGift = function() {
var gname = $('#giftname').val();
//should be getting gift options from webservice
var newGift = {name: gname,
packs: [{pack:'Each', packprice: '2'}, {pack:'Dozen', packprice: '12'}], price: {pack:'', packprice: ''}};
self.addGift(newGift);
$('#giftname').val('');
};
};
var viewModel = new GiftModel([]);
ko.applyBindings(viewModel);
</script>
When I add gifts, it creates options of packs. Each pack has a certain price. My problem is just simple. How will I show the price on the next column for the selected pack of the gift line? Sorry im just new to knockoutjs. Thanks!

System will automatically update price after selecting package if you make it observable. I made a little refactoring to you code, now it works:
<div>
<form>
<p>You have asked for <span data-bind='text: gifts().length'> </span> gift(s)</p>
<input
id='giftname' type='text' data-bind='value: giftName' />
<button data-bind='click: createGift'>Add Gift</button>
<table>
<thead>
<tr>
<th>Gift name</th>
<th>Pack</th>
<th>Price</th>
<th />
</tr>
</thead>
<tbody data-bind='foreach: gifts'>
<tr>
<td>
<input data-bind='value: name' readonly='readonly' />
</td>
<td>
<select data-bind="options: packs,
optionsText: 'pack',
optionsValue: 'packprice',
value: price" />
</td>
<td>
<input data-bind="value: price() || 'unknown'" readonly="readonly"
/>
</td>
<td><a href='#' data-bind='click: $root.removeGift'>Delete</a>
</td>
</tr>
</tbody>
</table>
<button data-bind='enable: gifts().length > 0' type='submit'>Submit</button>
</form>
</div>
function GiftModel(name) {
var self = this;
self.name = ko.observable(name);
self.price = ko.observable();
self.packs = ko.observable([{
pack: 'Each',
packprice: '2'
}, {
pack: 'Dozen',
packprice: '12'
}]);
}
var ViewModel = function (gifts) {
var self = this;
self.gifts = ko.observableArray(gifts);
self.giftName = ko.observable();
self.removeGift = function (gift) {
self.gifts.remove(gift);
};
self.createGift = function () {
self.gifts.push(new GiftModel(self.giftName()));
};
};
var viewModel = new ViewModel([]);
ko.applyBindings(viewModel);
Here is working fiddle: http://jsfiddle.net/vyshniakov/pzJaH/
P.S. Don't use jQuery to get field value if you using knockout. It is much easier to do this with knockout.

Related

PayPal API PopUp closes immediately after clicking PayPal button (Sandbox)

I am trying to implement the PayPal API into my Django / Vue checkout system, but everytime i try to access checkout via the paypal checkout buttons, thepopup closes immediately and i get these errors:
Error messages in developer tools
Obviously it has something to do with the cart's items' attributes and i tried to adjust them accordingly, but i can't figure out how to fix it. My Code:
<template>
<div class="page-checkout">
<div class="columns is-multiline">
<div class="column is-12">
<h1 class="title">Kasse</h1>
</div>
<div class="column is-12 box">
<table class="table is-fullwidth">
<thead>
<tr>
<th>Artikel</th>
<th>Preis</th>
<th>Anzahl</th>
<th>Gesamt</th>
</tr>
</thead>
<!-- vue for loop for looping through items in cart and displaying them in a table -->
<tbody>
<tr
v-for="item in cart.items"
v-bind:key="item.product.id"
>
<td>{{ item.product.name }}</td>
<td>{{ item.product.price }}€</td>
<td>{{ item.quantity }}</td>
<td>{{ getItemTotal(item).toFixed(2) }}€</td>
</tr>
</tbody>
<tfoot>
<tr style="font-weight: bolder; font-size: larger;">
<td colspan="2">Insgesamt</td>
<td>{{ cartTotalLength }}</td>
<td>{{ cartTotalPrice.toFixed(2) }}€</td>
</tr>
</tfoot>
</table>
</div>
<!-- Fields for user information -->
<div class="column is-12 box">
<h2 class="subtitle">Versanddetails</h2>
<p class="has-text-grey mb-4">*Felder müssen ausgefüllt sein</p>
<div class="columns is-multline">
<div class="column is-6">
<div class="field">
<label>*Vorname</label>
<div class="control">
<input type="text" class="input" v-model="first_name">
</div>
</div>
<div class="field">
<label>*Nachname</label>
<div class="control">
<input type="text" class="input" v-model="last_name">
</div>
</div>
<div class="field">
<label>*E-Mail</label>
<div class="control">
<input type="email" class="input" v-model="email">
</div>
</div>
<div class="field">
<label>*Telefonnummer</label>
<div class="control">
<input type="text" class="input" v-model="phone">
</div>
</div>
<div class="column is-6">
<div class="field">
<label>*Adresse</label>
<div class="control">
<input type="text" class="input" v-model="address">
</div>
</div>
<div class="field">
<label>*Postleitzahl</label>
<div class="control">
<input type="text" class="input" v-model="zipcode">
</div>
</div>
<div class="field">
<label>*Ort</label>
<div class="control">
<input type="text" class="input" v-model="place">
</div>
</div>
</div>
<!-- looping through errors for authorization -->
<div class="notification is-danger mt-4" v-if="errors.length">
<p v-for="error in errors" v-bind:key="error">{{ error }}</p>
</div>
<hr>
<div id="card-element" class="mb-5"></div>
<!-- Part for PayPal implementation -->
<template v-if="cartTotalLength">
<hr>
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
<!-- Include the PayPal JavaScript SDK -->
<div ref="paypal"></div>
<!-- <button class="button is-dark" #click="submitForm">Weiter mit PayPal</button> -->
</template>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'Checkout',
data() {
return {
loaded: false,
paidFor: false,
cart: {
items: []
},
stripe: {},
card: {},
first_name: '',
last_name: '',
email: '',
phone: '',
address: '',
zipcode: '',
place: '',
errors: []
}
},
mounted() {
document.title = 'Kasse | RELOAD'
this.cart = this.$store.state.cart
// Imported PayPal functionality from https://fireship.io/lessons/paypal-checkout-frontend/
const script = document.createElement("script");
script.src = "https://www.paypal.com/sdk/js?client-id=AeAKmRDX7HTesdUvfqYqCQz3fZHIkjAoQ5-BG6K-7xnL5GBMVvwQba53v-I8Fx1p9wurUtoBuk7D6bV1"; // Replace YOUR-CLIENT-ID with paypal credentials
script.addEventListener("load", this.setLoaded);
document.body.appendChild(script); // Adds script to the document's body
},
watch: {
$route(to, from,) {
if (to.name === 'Category') {
this.getCategory()
}
}
},
methods: {
getItemTotal(item) {
return item.quantity * item.product.price
},
// Authorization methods
submitForm() {
this.errors = []
if (this.first_name === '') {
this.errors.push('Bitte Vornamen angeben')
}
if (this.last_name === '') {
this.errors.push('Bitte Nachnamen angeben')
}
if (this.email === '') {
this.errors.push('Bitte E-Mail angeben')
}
if (this.phone === '') {
this.errors.push('Bitte Telefonnummer angeben')
}
if (this.adress === '') {
this.errors.push('Bitte Adresse angeben')
}
if (this.zipcode === '') {
this.errors.push('Bitte Postleitzahl angeben')
}
if (this.place === '') {
this.errors.push('Bitte Ort angeben')
}
},
// Imported PayPal functionality from https://fireship.io/lessons/paypal-checkout-frontend/
setLoaded() {
this.loaded = true;
window.paypal
.Buttons({
// createOrder: (data, actions) => {
// return actions.order.create({
// purchase_units: [
// {
// description: this.product.description,
// amount: {
// currency_code: "EUR",
// value: this.product.price
// }
// }
// ]
createOrder: (data, actions) => {
return actions.order.create({
"purchase_units": [{
"amount": {
"currency_code": "EUR",
"value": item.product.price,
"breakdown": {
"item_total": { /* Required when including the items array */
"currency_code": "EUR",
"value": item.quantity * product.price
}
}
},
"items": [
{
"name": item.product.name, /* Shows within upper-right dropdown during payment approval */
"description": item.product.description, /* Item details will also be in the completed paypal.com transaction view */
"unit_amount": {
"currency_code": "EUR",
"value": item.product.price
},
"quantity": item.product.quantity
},
]
}]
});
},
onApprove: async (data, actions) => {
const order = await actions.order.capture();
this.paidFor = true;
console.log(order);
},
onError: err => {
console.log(err);
}
})
.render(this.$refs.paypal);
}
},
computed: {
// Computing total price and total amount of all items
cartTotalPrice() {
return this.cart.items.reduce((acc, curVal) => {
return acc += curVal.product.price * curVal.quantity
}, 0)
},
cartTotalLength() {
return this.cart.items.reduce((acc, curVal) => {
return acc += curVal.quantity
}, 0)
}
}
}
</script>
The createOrder function inside setLoaded() is assigning properties using item although it's not clear where item comes from as it's not being passed in from anywhere:
setLoaded() {
this.loaded = true;
window.paypal.Buttons({
createOrder: (data, actions) => {
return actions.order.create({
purchase_units: [
{
amount: {
currency_code: "EUR",
value: item.product.price, // <--- item is not defined here
breakdown: {
item_total: {
currency_code: "EUR",
value: item.quantity * product.price, // <--- or here
},
},
},
...
If you mean to reference the items in the cart data property you need to use this.cart.items and possibly loop through it if you're trying to send individual item details.

Facing issues with passing list of Ids from one View to another controller

I am trying to pass a list of int Ids from one View to another on button click. I am able to hit the controller but its passing null.
Html:
#foreach (var business in Model.Businesses)
{
<tr>
<td>#business.Name</td>
<td>
<p>
#foreach (var bt in #business.BusinessTypes)
{
#bt.Name;
}
</p>
</td>
<td><button type="button" id="btnCalcVS"> Calculate Validator Score</button></td>
</tr>
}
JQUery:
<script type="text/javascript">
$(document).ready(function () {
var businessTypeIds = [];
$(document).on("click", "#btnCalcVS", function () {
$.ajax({
type: 'POST',
url: '/BusinessType/Index',
contentType: "application/json",
data: JSON.stringify( #foreach (var business in Model.Businesses)
{
#foreach (var bt in #business.BusinessTypes)
{
#bt.Id;
}
}),
success: function (data) {
alert("success");
},
error: function (e) {
alert(e);
}
});
});
});
</script>
Controller:
[HttpPost]
[Route("/BusinessType/Index")]
public IActionResult Index([FromBody] List<int> businessTypeIds)
{
//logic
}
As mentioned, I am hitting the controller but it's having null values. As can be seen from the HTML code, i have a list inside a list. (Business Types inside Businesses, so 2 many to many relationships)
Can someone tell me where i am going wrong?
You could try get the id from html and then pass it to controller.
<form method="post">
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].Id)
</th>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].CisloZakazky)
</th>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].CisloProduktu)
</th>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].Poradi)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.DenniPlan2)
{
<tr>
<td>
#Html.EditorFor(model => item.Id)
</td>
<td>
#Html.EditorFor(model => item.CisloZakazky)
</td>
<td>
#Html.EditorFor(model => item.CisloProduktu)
</td>
<td>
#Html.EditorFor(model => item.Poradi)
</td>
</tr>
}
</tbody>
</table>
<input type="button" id="btnCalcVS" value="submit" />
</form>
#section Scripts{
<script type="text/javascript">
$(document).ready(function () {
var businessTypeIds = [];
$("[id=item_Id]").each(function () {
businessTypeIds.push($(this).val());
});
console.log(businessTypeIds);
$(document).on("click", "#btnCalcVS", function () {
$.ajax({
type: 'POST',
url: '/BusinessType/Index',
contentType: "application/json",
data: JSON.stringify(businessTypeIds),
success: function (data) {
alert("success");
},
error: function (e) {
alert(e);
}
});
});
});
</script>
}
For this line $("[id=item_Id]").each(function () {, you could get the id value item_Id by check the generated html in the web browser.

coldfusion replace function not working correctly

Here is a document, stored in a variable called templlet:
{{d
{{h
Dear Customer,
We were so pleased that you have signed up for one of our programs. Below please find a listing of what you signed up for.
{{r
Sincerely Yours,
John Jones
President
XYZ Corporation
I intend to replace the {{d {{h and {{r with information selected by the user. {{d is a date, {{h is a heading and {{r is a report. The code to do this replacement is:
<cfset templlet = Replace(templlet, "{{d", "#repdatemm#")>
<cfset templlet = Replace(templlet, "{{h", "#heading#")>
<cfset templlet = Replace(templlet, "{{r", "#letrep#")>
The {{d and {{h are working fine. The {{r is being replaced with letrep, but not where it is positioned in templlet. Here is sample output:
July 06, 2018
Mr. Joseph Smith
1632 S. Bailey St.
Philadelphia, PA 19145
Dear Customer,
We were so pleased that you have signed up for one of our programs. Below please find a listing of what you signed up for.
Sincerely Yours,
John Jones
President
XYZ Corporation
Activity Code....Amount.....Note
EarlyReg.........500.00.....by check
Parking ...........30.00.......by paypal
Report Total :...530.00
(sorry for all the dots here -- I couldn't figure out how to put spaces in).
You can see that the {{r in templlet is prior to the signature, but the report is appearing after the signature line.
I've tried leaving more room in templlet between the {{r and the signature, which made no difference. I tried putting the {{r in a div, which made no difference. I'm baffled about why this is coming out in the wrong place. Can anyone tell me what is going wrong here?
The problem is that the:
<table>
Enclosing the report is not closed properly.
This works:
<cfsavecontent variable="content">
<link rel="stylesheet" href="sample.css"> <link rel="stylesheet" type = "text/css" href ="betty.css"/> <script type = "text/javascript"> fieldsplitput('moxlet', '�', 'x_1', 1) </script> <p style="margin-left:40px">{{d</p> <p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">{{h</span></span></p> <p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">Dear Customer,</span></span></p> <p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">We were so pleased that you have signed up for one of our programs. Apparently you live in the city of Philadelphia. Additionally we observe that you were referred to us by 191. Below please find a listing of what you signed up for.</span></span></p> <div> <p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif"> <link rel="stylesheet" type = "text/css" href ="betty.css"/> <link rel="stylesheet" type = "text/css" href ="persrep.css"/> <style type="text/css"> #media print { table tr.page-break{page-break-before:always} } </style> <HTML> <head> <link rel="stylesheet" type = "text/css" href ="betty.css"/> </head> <body> </body> </html> <div style = "margin:auto;overflow:auto; " >{{r</span></span></p> </div> <p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">Sincerely Yours,</span></span></p> <p style="margin-left:40px"><span style="font-size:14px"><span style="font-family:georgia,serif">John Jones<br /> President<br /> XYZ Corporation</span></span></p>
</cfsavecontent>
<cfset date = "July 06, 2018">
<cfsavecontent variable="header">
Mr. Joseph Smith<br />
1632 S. Bailey St.<br />
Philadelphia, PA 19145
</cfsavecontent>
<cfsavecontent variable="report">
<table class = "reptable" id = 'reptable' > <tr><td> </td></tr> <td> </td> <td class = "repcolhead1">Activity Code</td> <td class = "repcolhead1">Amount Paid</td> <td class = "repcolhead1">Note</td> <td><input type = "text" id = "repfoc" class = "inpbl" value = "" style = "lineheight: 2px; width:2px" > </td> </tr> <script type = "text/Javascript"> thefocus('repfoc') function thefocus(id) { //alert("got to thefocus id is " + id); document.getElementById(id).focus(); } </script> <td style = "padding-left: 0px; padding-top: 10px " class = "repsubh"> State: PA </td> <tr> <!--up counter on this subhead where appropriate ---> <td style = "padding-left: 10px; " class = "repsubh"> Trans. Date: 11-06-2017 </td> <!--up counter on this subhead where appropriate ---> <td class = "repcolrow" style = "color: #141212; text-align: left;" > EarlyReg </td> <td class = "repcolrow" style = "color: #141212; text-align: right;" > 500.00 </td> <td class = "repcolrow" style = "color: #141212; text-align: left;" > </td> <tr> <!--up counter on this subhead where appropriate ---> <td style = "padding-left:10px; vertical-align: middle; padding-top: 5px; padding-bottom:20px; " class = "repsubf"> Subtotal: 11-06-2017 </td> <!--up counter on this subhead where appropriate ---> <td class = "repcolrow" style = "text-align: left;; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> </a> </td> <script> placecount('cnt-2-1','cnt-2-2', 'brk-2-4-1', 'brk-2-4-2') </script> <td class = "repcolrow" style = "text-align: right;; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> 500.00 </a> </td> <td class = "repcolrow" style = "text-align: left;; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> </a> </td> <tr> <td style = "padding-left:0px; vertical-align: middle; padding-top: 5px; padding-bottom:20px; " class = "repsubf"> Subtotal: PA </td> <!--up counter on this subhead where appropriate ---> <!--up counter on this subhead where appropriate ---> <td class = "repcolrow" style = "text-align: left;; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> </a> </td> <script> placecount('cnt-3-1','cnt-3-2', 'brk-3-4-1', 'brk-3-4-2') </script> <td class = "repcolrow" style = "text-align: right;; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> 500.00 </a> </td> <td class = "repcolrow" style = "text-align: left;; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> </a> </td> <tr> <td class = 'repsubf' style = "font-weight:bold"> Report Total : <br> <a class = "repnum" id = "cnt-4-1" style = ""> Count : </a> <a class = "repnum" id = "cnt-4-2" style = "; ">1 </a> </p> </td> <td class = "repcolrow" style = "text-align: left;font-weight:bold; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> </a> </td> <script> placecount('cnt-4-1','cnt-4-2', 'brk-4-4-1', 'brk-4-4-2') </script> <td class = "repcolrow" style = "text-align: right;font-weight:bold; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> 500.00 </a> </td> <td class = "repcolrow" style = "text-align: left;font-weight:bold; vertical-align:top; padding-top: 10px color:630D85 " > <a class = "repbordtop" style = "position:relative; top:4px"> </a> </td> <tr> <tr><td> </td></tr><table>
</cfsavecontent>
<cfset content = ReplaceNoCase(content,"{{d",date)>
<cfset content = ReplaceNoCase(content,"{{h",header)>
<cfset content = ReplaceNoCase(content,"{{r",report)>
<cfoutput>
#content#
</cfoutput>

mootools 1.4.0.1 - htmltable push

in according with
http://mootools.net/more/docs/1.5.1/Interface/HtmlTable#HtmlTable
I have used push to add new rows, with some properties and positioning.
Here a snippet:
var table = new HtmlTable($('my_table_id'));
var target = "some code...";
table.push([
td_val1,
td_val2,
td_val3,
td_val4,
{
content: td_val5,
properties: {
'class': 'my_class'
}
},
td_val6,
],
(target !== undefined) ? {} : null,
(target !== undefined) ? target.parentNode : null,
(target !== undefined) ? 'td' : null,
(target !== undefined) ? 'after' : null
);
It work well with a empty table like (table.empty() before push):
<table id="my_table_id">
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
<th>Col6</th>
<th>Col7</th>
<th>Col8</th>
</tr>
</thead>
<tbody>
<tr class="no_result">
<td colspan="8">No results</td>
</tr>
</tbody>
</table>
But no effect on a pre fill table like:
<table id="my_table_id">
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
<th>Col6</th>
<th>Col7</th>
<th>Col8</th>
</tr>
</thead>
<tbody>
<tr>
<td>val1</td>
<td>val2</td>
<td>val3</td>
<td>val4</td>
<td>val5</td>
<td>val6</td>
<td>val7</td>
<td>val8</td>
</tr>
</tbody>
</table>
The push code is in a try/catch, but no excepetion raised.
I no focused the problem what is... Thanks so much for help.

Angular refresh list after post using Django-Rest-Framework

I've been looking on the forum how to update a list of items after the post using a server. The available examples show how to update the list but in memory.
This is my current code:
$http.get('job/').
success(function(data, status, headers, config) {
$scope.jobs = data;
}).
error(function(data, status, headers, config) {
console.log('Error', status);
});
$scope.save = function(job) {
$http.post('job/', job)
.success(function(data) {
$scope.jobs.push(data); // here i trying update the list in view
}).
error(function(data, status, headers, config) {
console.log('Error', status);
});
};
// HTML List
<tr ng-repeat="job in jobs">
{% verbatim %}
<td> {{ job.name }}</td>
<td>{{ job.description }}</td>
{% endverbatim %}
<td><a ng-click="edit(job.id)" class="btn btn-small btn-primary">edit</a></td>
<td><a ng-click="delete(job.id)" class="btn btn-small btn-danger">delete</a></td>
</tr>
// HTML form
<div class="span6" ng-controller="JobCtrl">
<h5>Create a New Job</h5>
<form class="form-inline">
<div class="form-group block-level">
<input type="text" class="form-control" name="name" ng-model="job.name" placeholder="Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="description" ng-model="job.description" placeholder="Description">
</div>
<div class="form-group">
<button class="btn btn-default" ng-click="save(job)">Add Job</button>
</div>
</form>
</div>
Thanks!
Solved using Observer.
myApp.service('JobService', function($http) {
var observerCallbacks = [];
// register the observer's callback in our callback list.
this.registerObserverCallback = function(callback){
observerCallbacks.push(callback);
};
// function to notify our observers and call the callback they registered with
var notifyObservers = function(){
angular.forEach(observerCallbacks, function(callback){
callback();
});
};
this.list = function() {
return $http.get('job/').
success(function(data) {
return data;
});
};
this.add = function(job) {
return $http.post('job/', job).success(function(data, status, headers, config) {
notifyObservers();
});
};
});
myApp.controller('JobCtrl', function ($scope, $http, $location, JobService) {
var getList = function() {
JobService.list().then(function(jobs){
$scope.jobs = jobs.data;
});
};
JobService.registerObserverCallback(getList);
getList();
$scope.save = function(job) {
JobService.add(job).success(function(data) {
$scope.job = '';
console.log('success!');
});
};
});
Here is the working solution (without BE communication). I think your code is fine, because it's working here, but I think you miss table tag around tr and td
http://jsfiddle.net/U3pVM/5031/
<table>
<tr ng-repeat="job in jobs">
<td> {{ job.name }}</td>
<td>{{ job.description }}</td>
<td><a ng-click="edit(job.id)" class="btn btn-small btn-primary">edit</a></td>
<td><a ng-click="delete(job.id)" class="btn btn-small btn-danger">delete</a></td>
</tr>
</table>
You don't have to do any $scope.$apply, because you are already working within angular scope (ng-click and $http backend request are that)