its my first time using angular with firebase.
I am having a hard time saving data as an object to firebase.
whenever i save a data, it doesn't provide index id or random generated id.
so first time name it saves the data and second time it overwrites the data because of not having any id.
here is my code
myApp.controller('MeetingsController', ['$scope','$firebaseObject', function ($scope, $firebaseObject) {
var ref = new Firebase('https://saddam.firebaseio.com/employees');
var obj = $firebaseObject(ref);
$scope.meetings = obj;
$scope.meetings.$loaded(function() {
console.log(obj);
});
$scope.addMeeting = function() {
obj.userinfo= {
meetingName : $scope.meetingname,
date : Firebase.ServerValue.TIMESTAMP
}
obj.$save();
}
}]);
Looking for some help
Related
I need to insert in my Firebase using AngularJS a new child with value 0.
I try to insert using this command but doesn't work:
var ref = new Firebase(FBURL);
objectname = "name";
$scope.insert= ref.child("user/"+objectname);
$scope.insert.setvalue("Arthur");
result expected
firebaseID
|-------------user
|-----name:Arthur
You're not doing it the Angular way.
https://github.com/firebase/angularfire/blob/master/docs/guide/README.md
You could use Three-way Data Bindings:
var app = angular.module("sampleApp", ["firebase"]);
// a factory to create a re-usable Profile object
// we pass in a username and get back their synchronized data as an object
app.factory("Profile", ["$firebaseObject",
function($firebaseObject) {
return function(username) {
// create a reference to the database node where we will store our data
var ref = firebase.database().ref("rooms").push();
var profileRef = ref.child(username);
// return it as a synchronized object
return $firebaseObject(profileRef);
}
}
]);
app.controller("ProfileCtrl", ["$scope", "Profile",
function($scope, Profile) {
//your object id
var objectId = '-KSe_RHPYjhkjasjjh-o6';
// create a three-way binding to our Profile as $scope.profile
Profile(objectId).$bindTo($scope, "profile");
//now simple manipulate the $scope.profile variable
$scope.profile.name = "Arthur";
}
]);
I'm breaking my head on this one.
From my backend I fetch json data for my typeahead input field.
once a value is selected I fetch the full object from the backend by using the unique id that came with the auto complete response.
next thing i do is to push this object into an array and then i itterate over this array to display in the html all fine.
but i would like to test first if the object is already in the array.
for that i'm using array.indexOf property, however for some reason it will never find a match: i hope someone can help me:
here is the code:
the factory:
productServices.factory('Product', ['$resource',
function($resource){
return $resource('http://127.0.0.1:5000/products/:id', {id: '#id'}, {
query: { method:'GET',
isArray:false,
responseType:'json',
cache:true},
update: { method:'PUT' }
fetch product from backend and push to an array:
$scope.onSelect = function ($item, $model, $label) {
$scope.$item = $item
// call the .addProduct service to push the product.get result into a list
acProductService.addProduct(Product.get({ id: $scope.$item.id }));
// this is to retrieve the list
$scope.products = acProductService.getProducts();
// reset field
$scope.selected = '';
};
service to push the data to the array and to list the array (so here on addProduct i would like to test if the object is already in the array)
autocompServices.service('acProductService', function() {
var productList = [];
var addProduct = function(newObj) {
console.log(productList.indexOf(newObj)); ---- here is my issue it will never find the newObj despite i've added it many times
productList.push(newObj);
console.log(productList.indexOf(newObj)); -- does work but i need to check before pushing to the list and not after;
};
var getProducts = function(){
return productList;
};
return {
addProduct: addProduct,
getProducts: getProducts
};
i'm guessing the $resource.get returns something that is unique irrespective of the object retrieved from the back-end is actually unique. any help please.. thanks
I have an app where I need to store artists and their details in database.Now I want to retrieve all the artists and render some of their details in front end.How to do that.
Secondly, if I get the artist rating in some input field by using ng-model, then how to store that value in a particular artist to update details.
The database structure is:
{
"artists": {
"Atif":{
"name":"atif",
"rating":8
},
"Himesh":{
"name":"himesh",
"rating":5
}
}
}
and this is angular.js
(function()
{
var app = angular.module("myapp", ["firebase"]);
app.controller("maincontroller", function($scope, $firebaseObject,$firebaseArray)
{
var ref = new Firebase("https://gigstart.firebaseio.com/");
var artists=ref.child("artists");
// download the data into a local object
$scope.data = $firebaseObject(ref);
// putting a console.log here won't work, see below
ref.on("value", function(snapshot)
{
console.log(snapshot.val());
}, function (errorObject)
{
console.log("The read failed: " + errorObject.code);
});
var artistsRef=new Firebase("https://gigstart.firebaseio.com//artists");
}); //end of controller
Now I want to render the name and rating of each artist in front end.Can I do something like
<div ng-repeat="artist in artists">
{{artist.name}}
{{artist.rating}}
</div>
You have a list of artists, which you want to ng-repeat over in your Angular view. You can accomplish that by:
app.controller("maincontroller", function($scope, $firebaseArray)
{
var ref = new Firebase("https://gigstart.firebaseio.com/");
var artists = ref.child("artists");
$scope.artists = new $firebaseArray(artists);
}
Please take a moment to go through the AngularFire quickstart before starting on your own project. This is covered in step 5.
I am adding new children to a Firebase list and unique IDs are automatically created. I need some way to get the ID thats been created so I can immediately change the URL location path to a route containing that ID. Here is how I am creating the list items:
.controller('NewTestCtrl', ['$scope', 'syncData', '$location', function($scope, syncData, $location) {
$scope.newTitle = null;
$scope.tests = syncData('tests');
$scope.createTest = function() {
var newDate = new Date();
if( $scope.newTitle ) {
$scope.tests.$add({
title: $scope.newTitle,
createdDate: newDate,
modified: newDate,
user: $scope.auth.user.uid
});
$location.path("/test/" + [FIREBASE UNIQUE ID] + "/1");
}
};
}])
Thanks for your help!
The angularFire $add method returns a promise, which when resolved will contain a Firebase reference to your newly pushed value. With that value, you can get the UID.
$scope.tests.$add({
...your object....
})
.then(function(ref) {
$location.path("/test/"+ref.name());
});
I am trying to use, https://github.com/jeromegn/Backbone.localStorage, to store models. I ve got a JSON and I am fetch data with backbone and I am trying to local store the fetched data. The first variable of json file is a time key called tstamp. How can I store and retrieve data based on tstamp attribute?? My code:
// Backbone model Creation for highlight
var HighlightModel = Backbone.Model.extend({
defaults: {
tstamp: "1234",
att: "",
},
initialize: function () {
}
});
//Backbone model initialization
highlight = new HighlightModel();
var HighlightList = Backbone.Collection.extend({
model: HighlightModel,
localStorage: new Backbone.LocalStorage("highlightList"),
url: 'data.json'
});
var HighlightView = Backbone.View.extend({
el: "#highlights",
template: _.template($('#highlightTemplate').html()),
render: function (eventName) {
_.each(this.model.models, function (highlight) {
var highlightTemplate = this.template(highlight.toJSON());
//push data to obj for highlight script
mp = highlight.toJSON();
// Add data to DOM element
$(this.el).html(highlightTemplate);
}, this);
return this; // .remove(); to stop displaying
}
});
var highlights = new HighlightList([highlight]);
var highlightsView = new HighlightView({
model: highlights
});
// Fetching data from server every n seconds
setInterval(function () {
highlights.fetch({
reset: true
});
highlight.add(sentiments);
highlights.save();
}, htCycle); // Time in milliseconds
highlights.bind('reset', function () {
highlightsView.render();
console.log('render');
});
EDIT:
I change my code a little bit, now i have at local storage just store the default model {"tstamp":"1234","att":"","id":"4fb1b437-0e37-8eb7-ed3c-cbd9d0dcff98"}. I want to store the fetched data from server to localstorage.