Permalink
Browse files

observable arrays added

  • Loading branch information...
1 parent 549ee91 commit 19e5639920902e432e989680ca1939d96b85220a @subsonic subsonic committed Jul 4, 2012
@@ -1,6 +1,6 @@
@helper LinksForStatus(MvcMusicStore.Models.Order order){
if(order.Status == "paid"){
- <form id="statusForm" action="/orders/refund" method="post" style="display:inline">
+ <form id="statusForm" action="/orders/refund" method="post" style="display:inline" data-bind = "visible : canRefund(), click : refundOrder">
<input type="hidden" name="id" value="@order.OrderId" />
<li>@Forms.SubmitLink("Refund", "icon-repeat")</li>
</form>
Binary file not shown.
Binary file not shown.
@@ -328,6 +328,7 @@
<Content Include="assets\images\album_art\47.jpg" />
<Content Include="assets\images\album_art\48.jpg" />
<Content Include="assets\images\album_art\49.jpg" />
+ <Content Include="assets\images\album_art\4_2.jpg" />
<Content Include="assets\images\album_art\5.jpg" />
<Content Include="assets\images\album_art\50.jpg" />
<Content Include="assets\images\album_art\51.jpg" />
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
- <ProjectView>ShowAllFiles</ProjectView>
+ <ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
@@ -64,4 +64,5 @@ button.link
{
background-color : transparent;
border:0;
-}
+}
+
@@ -1,19 +1,48 @@
-var orderViewModel = function (order) {
+var OrderManager = function (order) {
var self = this;
-
+ self.OrderId = ko.observable(order.OrderId);
self.Email = ko.observable(order.Email);
self.FirstName = ko.observable(order.FirstName);
self.LastName = ko.observable(order.LastName);
+ self.Status = ko.observable(order.Status);
self.FullName = ko.computed(function () {
return self.FirstName() + " " + self.LastName();
}, this);
- self.Notes = order.Notes;
+ self.Notes = ko.observableArray(order.Notes);
+ self.Transactions = ko.observableArray(order.Transactions);
self.editNoteUrl = function (id) {
return "/ordernotes/edit/" + id;
}
self.deleteNote = function (note) {
- alert("going to delete note " + note.Id);
+ MusicStore.deleteNote(note, function (result) {
+ if (result.success)
+ self.Notes.remove(note);
+ });
+ };
+ self.canRefund = ko.computed(function () {
+ return self.Status() === "paid";
+ });
+
+ self.refundOrder = function () {
+ return MusicStore.refundOrder(order, function (result) {
+ //load the status
+ //transactions
+ //notes
+ self.Status(result.order.Status);
+ self.Transactions(result.order.Transactions);
+ self.Notes(result.order.Notes);
+ });
+ };
+
+ self.addNote = function () {
+ var note = $("#newNote").val();
+ var newNote = { OrderId: self.OrderId(), Note: note };
+ MusicStore.addNote(newNote, function (result) {
+ console.log(result);
+ if (result.success)
+ self.Notes.push(result.newNote);
+ });
};
self.parseDate = function (jsonDate) {
var re = /-?\d+/;
@@ -25,7 +54,6 @@
return curr_month + "-" + curr_date + "-" + curr_year + " " + d.getHours() + ":" + d.getMinutes();
};
-
self.saveOrder = function () {
var data = $("#orderForm").serialize();
@@ -38,3 +66,56 @@
});
};
};
+
+MusicStore = function () {
+
+ var _notify = function (result) {
+ if (result.message) {
+ if (result.success) {
+ notifier.success(result.message);
+ } else {
+ notifier.error(result.message);
+ }
+ }
+ };
+
+ var _canRefund = function (order) {
+ return order.Status === "paid";
+ };
+ var _refundOrder = function (order, callback) {
+ $.post("/orders/refund/", { id: order.OrderId }, function (result) {
+ _notify(result);
+ callback(result);
+ });
+ };
+ var _deleteNote = function (note, callback) {
+ $.post("/ordernotes/delete/", { id: note.Id }, function (result) {
+ _notify(result);
+ callback(result);
+ });
+ };
+ var _addNote = function (newNote, callback) {
+ $.post("/ordernotes/create/", newNote, function (result) {
+ _notify(result);
+ callback(result);
+ });
+ };
+
+ return {
+ deleteNote: _deleteNote,
+ addNote: _addNote,
+ canRefund: _canRefund,
+ refundOrder : _refundOrder
+ }
+
+} ();
+
+var loadOrder = function () {
+ orderManager = new OrderManager(window.order);
+ ko.applyBindings(orderManager);
+
+};
+
+$().ready(function () {
+ loadOrder();
+});
Binary file not shown.
Binary file not shown.
@@ -36,11 +36,12 @@ public ActionResult Create(OrderNote item)
if (ModelState.IsValid)
{
db.OrderNotes.Add(item);
- return RedirectToAction("edit", "orders", new { id = item.OrderId });
+ item.CreatedOn = DateTime.Now;
+ return Json(new { success = true, message = "Note created", newNote = item});
}
else
{
- return View();
+ return Json(new { success = false, message = "Note invalid" });
}
}
@@ -65,8 +66,15 @@ public ActionResult Edit(OrderNote item)
public ActionResult Delete(int id)
{
var item = db.OrderNotes.Find(id);
- db.OrderNotes.Remove(item);
- return RedirectToAction("edit", "orders", new { id = item.OrderId });
+ try
+ {
+ db.OrderNotes.Remove(item);
+ return Json(new { success = true, message = "Note deleted" });
+ }
+ catch (Exception x)
+ {
+ return Json(new { success = false, message = "Can't delete order... there was an error" });
+ }
}
}
}
@@ -25,11 +25,29 @@ public ViewResult Index()
public ActionResult Refund(int id){
var order = db.Orders.Find(id);
- order.Status = "refunded";
- order.Notes = new List<OrderNote>();
- order.Notes.Add(new OrderNote { Note = "Order Refunded: Authorization XYZ by " + User.Identity.Name, CreatedOn = DateTime.Now });
- TempData["message"] = "Order Refunded";
- return RedirectToAction("edit", new {id = id});
+
+ try
+ {
+ order.Status = "refunded";
+ order.Notes.Add(new OrderNote { Note = "Order Refunded: Authorization XYZ by " + User.Identity.Name, CreatedOn = DateTime.Now });
+ order.Transactions.Add(new Transaction { Amount = -order.Total, Authorization = "XYZ by " + User.Identity.Name, CreatedOn = DateTime.Now, Discount = 0, Processor = "coupon" });
+ //push the changes, we need the changed object
+ db.SaveChanges();
+ //reload it
+ order = db.Orders
+ .Include("Transactions")
+ .Include("OrderDetails")
+ .Include("Notes")
+ .Where(x => x.OrderId == id).FirstOrDefault();
+ return Json(new { success = true, message = "Order Refunded", order = order });
+ }
+ catch
+ {
+ return Json(new { success = false, message = "Can't refund this order, there was an error", order = order });
+ }
+
+ //TempData["message"] = "Order Refunded";
+ //return RedirectToAction("edit", new {id = id});
}
public ActionResult Void(int id)
{
Binary file not shown.
Binary file not shown.
@@ -3,6 +3,7 @@
@{
ViewBag.Title = "Edit";
}
+
<script src="/assets/js/order_manager.js" type="text/javascript"></script>
<div id='normal'>
<div class='container'>
@@ -56,17 +57,29 @@
<span class="label" data-bind = "text : $parent.parseDate(CreatedOn)"></span>
</td>
<td >
- <a data-bind = "attr : {href : $parent.editNoteUrl(Id)}, text : Note"></a>
+ <span data-bind = "text : Note"></span>
+ </td>
</td>
<td>
<button class = "btn btn-danger" data-bind = "click : $parent.deleteNote">
<i class="icon icon-remove icon-white"></i>
</button>
-
</td>
</tr>
<!-- /ko -->
</tbody>
+ <tfoot>
+ <tr>
+ <td colspan=2>
+ <input id = "newNote" />
+ </td>
+ <td>
+ <button class="btn btn-info" data-bind = "click : addNote">
+ <i class="icon icon-white icon-plus-sign"></i>
+ </button>
+ </td>
+ </tr>
+ </tfoot>
</table>
</div>
</div>
@@ -119,30 +132,20 @@
</tr>
</thead>
<tbody>
- @foreach (var tx in @Model.Transactions.ToList())
- {
+ <!-- ko foreach:Transactions -->
<tr>
+ <td data-bind = "text : Processor"></td>
+ <td data-bind = "text : Amount"></td>
+ <td data-bind = "text : Authorization"></td>
+ <td data-bind = "text : $parent.parseDate(CreatedOn)"></td>
<td>
- @tx.Processor
- </td>
- <td>
- @tx.Amount.ToString("C")
- </td>
- <td>
- @tx.Authorization
- </td>
- <td>
- @tx.CreatedOn.ToShortDateString()
- </td>
- <td>
- <a href="@Url.Action("edit","transactions",new {id = tx.Id})" class="btn btn-info">
- <i class="icon icon-edit icon-white"></i>
- Edit
- </a>
- @Forms.DeleteButton(Url.Action("delete","transactions"),tx.Id)
+ <button class="btn btn-info" data-bind = "visible : $parent.canRefund(), click : $parent.refundOrder">
+ <i class="icon icon-white icon-repeat"></i>
+ Refund
+ </button>
</td>
</tr>
- }
+ <!-- /ko -->
</tbody>
</table>
@@ -215,10 +218,5 @@
@Forms.DeleteConfirmScript()
<script type="text/javascript">
-
- $().ready(function(){
- var order = @Html.Raw(ViewBag.OrderJson);
- ko.applyBindings(new orderViewModel(order));
- });
-
+ order = @Html.Raw(ViewBag.OrderJson);
</script>
@@ -7,15 +7,18 @@
<meta name="description" content="">
<meta name="author" content="Microsoft with the help of Tekpub">
- @{
- Bundles.Reference("jquery");
- Bundles.Reference("assets/js");
- Bundles.Reference("assets/css");
- }
- @Bundles.RenderScripts()
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
+ <script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.1.0.js"></script>
+ <script src="/assets/js/bootstrap.min.js"></script>
+ <script src="/assets/js/bootstrap-collapse.js"></script>
+ <script src="/assets/js/notifier.js"></script>
+
<!-- Le styles -->
- @Bundles.RenderStylesheets()
+ <link href="/assets/css/bootstrap.min.css" rel="stylesheet">
+ <link href="/assets/css/bootstrap-responsive.min.css" rel="stylesheet">
+ <link href="/assets/css/overrides.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>

0 comments on commit 19e5639

Please sign in to comment.