Reputation: 13
all. I'm new with Angular. I need to find out how to save my JSON changes after push.
app.controller('ReviewController', function(){
this.r = {};
this.addReview = function(product){
this.r.createdOn = Date.now();
product.reviews.push(this.r);
this.r = {};
};
});
also I have this thing, where I get JSON data. I don't know if it would be useful for you to know that
app.factory('events', function($http) {
return function(){
return $http.get('products.json');
};
});
app.controller("StoreController", function(events){
var store = this;
store.products = [];
events().success(function(data) {
store.products = data;
});
One more thing. My products.json file:
[
{
"title": "Rubinas",
"price": 199.99,
"description": "Labai kietas, davai pirkit.",
"images": {
"full": "images/gem2.gif",
"thumb": "images/gem2t.gif"
},
"reviews": [
{
"stars": 1,
"body": "Bullshit",
"author": "Hater",
"createdOn": 1397490980837
},
{
"stars": 3,
"body": "Simple stone!",
"author": "Dude",
"createdOn": 1397490980837
},
{
"stars": 5,
"body": "Amazing.",
"author": "Kate",
"createdOn": 1397490980837
}
],
"canPurchase": true,
"soldOut": false
},
{
"title": "Smaragdas",
"price": 499.99,
"description": "Oho, koks.",
"images": {
"full": "images/gem3.gif",
"thumb": "images/gem3t.gif"
},
"reviews": [
{
"stars": 5,
"body": "This is so amazing",
"author": "Steeler",
"createdOn": 1397490980837
},
{
"stars": 4,
"body": "Pretty cool!",
"author": "Jim",
"createdOn": 1397490980837
},
{
"stars": 2,
"body": "I don't like it very much.",
"author": "Kim",
"createdOn": 1397490980837
}
],
"canPurchase": true,
"soldOut": false
},
{
"title": "Deimantas",
"price": 9999.99,
"description": "Labai kietas, na bet rimtai.",
"images": {
"full": "images/gem4.gif",
"thumb": "images/gem4t.gif"
},
"reviews": [
{
"stars": 1,
"body": "Eww...",
"author": "Hater",
"createdOn": 1397490980837
},
{
"stars": 4,
"body": "Nice!",
"author": "Dude",
"createdOn": 1397490980837
},
{
"stars": 5,
"body": "So unbelievible.",
"author": "Matt",
"createdOn": 1397490980837
},
{
"stars": 2,
"body": "I don't like it.",
"author": "Steven",
"createdOn": 1397490980837
}
],
"canPurchase": true,
"soldOut": false
}
]
Upvotes: 0
Views: 2586
Reputation: 28590
The are some ways to do this
First : You can create a button say called "save changes", and tell the user after finishing the process of adding new data with your so-called function addReview, Hit the save changes , and in the on-click of this button , you can create a $http.post request and send your new pushed array into your database (or your json file , whatever it is)
Second : You can do the same $http.post in your own function , addReview, without creating a button and listening for user to click on save changes button.
For example: if you want to go for my second solution :
this.addReview = function(product){
this.r.createdOn = Date.now();
product.reviews.push(product);
this.r = {};
$http.post('yourServerFile',WhateverYouWantToSave)
.success(function(){
alert('Your Changes have been saved to database');
})
};
//Dont forget to inject $http into your controller
And its better to define a factory/servise for this kind of theories(pushing/adding/sending/deleting somethig from somewhere)
Upvotes: 1