mercredi 25 mars 2015

Parse Cloud Code Define and Job in one JavaScript file


I am running a few cloud code actions in my main.js file which is uploaded to Parse.com. I decided to add a job to my project so, just like all the other cloud code functions I am running in the main.js, I decided to write the job at the bottom of the file. All was well, it was successfully uploaded to the server, except one thing. When I went to schedule a job, it gave me the error, "You need to add a job in Cloud Code before you can schedule a job." Anyways, after trying a bunch of different solutions, and receiving no positive yield, I uploaded the job by itself. It worked perfectly and I was able to schedule just fine. What am I doing wrong in my code below to cause the jobs and the code functions to not all run in the same js file? I heard I might need to link the main.js to another js file by doing something like 'var abc = require('cloud/cloudFunctions.js');'? Is that necessary?


Thanks



Parse.Cloud.define("updateScore", function(request, response) {
if (!request.user) {
response.error("Must be signed in to call this Cloud Function.")
return;
}
// Make sure to first check if this user is authorized to perform this change.
// One way of doing so is to query an Admin role and check if the user belongs to that Role.
// I've chosen to use a secret key. DO NOT use this method in a PUBLIC iOS app.
if (request.params.secret != "code1234") {
response.error("Not authorized.")
return;
}

// The rest of the function operates on the assumption that the request is *authorized*

Parse.Cloud.useMasterKey();

// Query for the user to be modified by objectId
// The objectId is passed to the Cloud Function in a
// key named "objectId". You can search by email or
// user id instead depending on your use case.

var query = new Parse.Query(Parse.User);
query.equalTo("objectId", request.params.objectId);

// Get the first user which matches the above constraints.
query.first({
success: function(anotherUser) {
// Successfully retrieved the user.
// Modify any parameters as you see fit.
// You can use request.params to pass specific
// keys and values you might want to change about
// this user.
anotherUser.set("totalScore", request.params.score);
anotherUser.set("WorLStreak", request.params.worlstreak);
anotherUser.set("WorLNumber", request.params.worlnumber);
anotherUser.set("FirstGoalName", request.params.firstGoalName);
anotherUser.set("WinningTeamSelection", request.params.winningTeamSelection);

// Save the user.
anotherUser.save(null, {
success: function(anotherUser) {
// The user was saved successfully.
response.success("Successfully updated user.");
},
error: function(gameScore, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
response.error("Could not save changes to user.");
}
});
},
error: function(error) {
response.error("Could not find user.");
}
});
});

Parse.Cloud.define("sendPushToUsers", function(request, response) {
var message = request.params.message;
var recipientUserId = request.params.recipientId;
var channelId = request.params.channelId;

if (!request.user) {
response.error("Must be signed in to call this Cloud Function.")
return;
}
// Make sure to first check if this user is authorized to perform this change.
// One way of doing so is to query an Admin role and check if the user belongs to that Role.
// I've chosen to use a secret key. DO NOT use this method in a PUBLIC iOS app.
if (request.params.secret != "code4321") {
response.error("Not authorized.")
return;
}

Parse.Cloud.useMasterKey();

// Validate the message text.
// For example make sure it is under 140 characters
if (message.length > 140) {
// Truncate and add a ...
message = message.substring(0, 137) + "...";
}

// Send the push.
// Find devices associated with the recipient user
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("channels", channelId);

// Send the push notification to results of the query
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
});


Parse.Cloud.define("sendPushToWinningUsers", function(request, response) {
var message = request.params.message;
var recipientUserId = request.params.recipientId;
var userId = request.params.userId;

if (!request.user) {
response.error("Must be signed in to call this Cloud Function.")
return;
}
// Make sure to first check if this user is authorized to perform this change.
// One way of doing so is to query an Admin role and check if the user belongs to that Role.
// I've chosen to use a secret key. DO NOT use this method in a PUBLIC iOS app.
if (request.params.secret != "codeJob1234") {
response.error("Not authorized.")
return;
}

Parse.Cloud.useMasterKey();

// Validate the message text.
// For example make sure it is under 140 characters
if (message.length > 140) {
// Truncate and add a ...
message = message.substring(0, 137) + "...";
}

// Send the push.
// Find devices associated with the recipient user
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("owner", userId);

// Send the push notification to results of the query
Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});
});

Parse.Cloud.job("gameTime", function(request, response) {
var message = "It’s Game Time! Tune in for live scoring updates!";

Parse.Cloud.useMasterKey();
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo("channels", "PreGameNotifications");

Parse.Push.send({
where: pushQuery,
data: {
alert: message
}
}).then(function() {
response.success("Push was sent successfully.")
}, function(error) {
response.error("Push failed to send with error: " + error.message);
});

var query = new Parse.Query("Score");
// query.equalTo("objectId", “”);

// Get the first user which matches the above constraints.
query.first({
success: function(anotherUser) {

anotherUser.set("isGameTime", "YES");

// Save the user.
anotherUser.save(null, {
success: function(anotherUser) {
response.success("Successfully updated user.");
},
error: function(gameScore, error) {
response.error("Could not save changes to user.");
}
});
},
error: function(error) {
response.error("Could not find user.");
}
});
});




jQuery Ready Event Not Working After AJAX Load


Excuse me...


Found any answer there and still didn't works.


So there is my scripts:



$(document).ready(function() {
$('.gifs').gifplayer();
});


And dynamic content loaded by AJAX:



<img class="gifs post img-responsive img-bordered" width="100%" src="<?php echo $post->thumb_url;?>" data-gif="<?php echo $post->url;?>" />


Image with class "gifs" should called "gifplayer" function. But everytime ajax loaded, it wont works. What I should do to get a best implementation for this?


That's all and thank you...





Adding List Item (Div) with a Variable from a Prompt (jQuery)


I am working on a class assignment, and each of the "Add Task" buttons need to have a prompt show up (which I have done), and the item that is entered needs to be added to the bottom of the respective lists.


I have tried the appendTo and append options, but I am not sure how to append my variables item1, and item2 to their respective lists.



$("#add1").click(function(){
var item1 = prompt ("What task would you like to give Kelly?");
});


Here's the problem via JSFiddle: here





stop ng repeat from makeing recursive call to tab ui select event


I have problem, when i create angualrjs tabs ui dynamically, the ng repeat will keep call select event function recursive and Pass county IDs and Call the Web API to get data. just want to ask for solution to stop ng repeat from passing county IDs and make API call? View



<tabset>
<tab ng-repeat="tab in countytabs" heading="{{tab.countyName}}" select="selectAllUserByCounty(tab.countyID)">
<h3>{{tab.countyName}}--{{tab.phoneNumber}} </h3>
<tabset>
<tab heading="All" active="active.all" select="selectAllUserByCounty(tab.countyID)">
<br />
<span>Total:{{totalStatusforByCounty.total}}, In:{{totalStatusforByCounty.in}}, Out:{{totalStatusforByCounty.out}}, Unknown: {{totalStatusforByCounty.unknown}} at {{totalStatusforByCounty.lastUpdatedDateTime | date:"MM/dd/yyyy 'at' h:mma"}} </span>
<br />
<div ng-repeat="groupUsers in allUserByCounty">
<h6>
<b>{{groupUsers.title}}</b>
</h6>
<table ng-repeat="user in groupUsers.users">
<tr>
<td>{{user.firstName}} {{user.lastName}} Ext:{{user.voiceMailExt}} </td>
</tr>
</table>
</div>
</tab>
<tab ng-repeat="departmentGroup in departmentGroups" heading="{{departmentGroup.name}}" select="selectAllUserByCountyAndDepartmentGroup(tab.countyID,departmentGroup.id)">
{{departmentGroup.name}}<br />
{{tab.countyID}}<br />
{{departmentGroup.id}}<br />

<div>
<p>
<span>
Total:{{totalStatusforByCountyAndDepartmentGroup.total}}, In:{{totalStatusforByCountyAndDepartmentGroup.in}}, Out:{{totalStatusforByCountyAndDepartmentGroup.out}}, Unknown: {{totalStatusforByCountyAndDepartmentGroup.unknown}} at {{totalStatusforByCountyAndDepartmentGroup.lastUpdatedDateTime | date:"MM/dd/yyyy 'at' h:mma"}}
</span>
</p>
</div>

<div ng-repeat="groupUsers in allUserByCountyAndDepartmentGroup">
<h6>
<b>{{groupUsers.title}}</b>
</h6>
<table ng-repeat="user in groupUsers.users">
<tr>
<td>{{user.firstName}} {{user.lastName}} Ext:{{user.voiceMailExt}} </td>
</tr>
</table>
</div>
</tab>
</tabset>
</tab>
</tabset>
</div>


javascript



(function(){
'use strict';

var app = angular.module('usersboard');

var ReceptionController = function($scope, ReceptionService){

$scope.countytabs = '';
$scope.totalStatusforAllCounties ='';
$scope.totalStatusforByCounty = '';
$scope.departmentGroups = '';
$scope.totalStatusforByCountyAndDepartmentGroup = '';
$scope.allUserByCountyAndDepartmentGroup = '';
$scope.allUserByCounty = '';
$scope.AllUserInAllDepartmentGroupsGroupByCounties = '';
$scope.AllUsersInDepartmentGroup= '';
$scope.active = {
all: false
};
$scope.content = 'county';
$scope.isShown = function (content) {
return content === $scope.content;
};

var selectAllCounties = function(){
ReceptionService.selectAllCounties().then(function(data){
$scope.countytabs = data;

}, function(errMsg){
console.log(errMsg);
});
}
selectAllCounties();

var selectTotalStatusforAllCounties = function(){
ReceptionService.selectTotalStatusforAllCounties().then(function(data){
$scope.totalStatusforAllCounties = data;
console.log(data);
}, function(errMsg){
console.log(errMsg);
});
}
selectTotalStatusforAllCounties();

var selectAllDepartmentGroups = function(){
ReceptionService.selectAllDepartmentGroups().then(function (data) {
$scope.departmentGroups = data;
console.log(data);
}, function (errMsg) {
console.log(errMsg);
});
}
selectAllDepartmentGroups();

$scope.selectTotalStatusforByCounty = function (id) {
if (typeof id !== 'undefined'){
ReceptionService.selectTotalStatusforByCounty(id).then(function (data) {
$scope.totalStatusforByCounty = data;
console.log($scope.totalStatusforByCounty);
}, function (errMsg) {
console.log(errMsg);
});
}

}
$scope.selectTotalStatusforByCountyAndDepartmentGroup = function (countyId, departmentGroup) {
ReceptionService.selectTotalStatusforByCountyAndDepartmentGroup(countyId, departmentGroup).then(function (data) {
$scope.totalStatusforByCountyAndDepartmentGroup = data;
console.log($scope.totalStatusforByCountyAndDepartmentGroup);
}, function (errMsg) {
console.log(errMsg);
});
}
$scope.selectAllUserByCountyAndDepartmentGroup = function (countyId, departmentGroup){
$scope.selectTotalStatusforByCountyAndDepartmentGroup(countyId, departmentGroup);
ReceptionService.selectAllUserByCountyAndDepartmentGroup(countyId, departmentGroup).then(function (data) {
$scope.allUserByCountyAndDepartmentGroup = data;

}, function (errMsg) {
console.log(errMsg);
});
}
$scope.selectAllUserByCounty = function (countyId) {

$scope.selectTotalStatusforByCounty(countyId);
ReceptionService.selectAllUserByCounty(countyId).then(function(data){
$scope.allUserByCounty = data;


}, function(errMsg){
console.log(errMsg);
});

}

$scope.selectAllUserInAllDepartmentGroups = function () {

ReceptionService.selectAllUserInAllDepartmentGroups().then(function (data) {
$scope.AllUserInAllDepartmentGroupsGroupByCounties = data;

}, function (errMsg) {
console.log(errMsg);
});
}
$scope.selectAllUsersInDepartmentGroups = function (departmentGroupId) {

ReceptionService.selectAllUsersInDepartmentGroup(departmentGroupId).then(function (data) {
$scope.AllUsersInDepartmentGroup = data;

}, function (errMsg) {
console.log(errMsg);
});
}


};

app.controller('ReceptionController', ['$scope', 'ReceptionService', '$window', ReceptionController]);

}());




How to update html path in yeoman angular on build


I've just learning yeoman and grunt. I have created a standard angular project using yeoman-angular generator. I want my folder structures by features instead of by type. for example, i have html in app/products/productlist.html and app/customers/customerlist.html. On build i want this html to be put to dist/templates folder. How can i tell grunt to update the html refs accordingly. Same with images. I put my images in app/assets/images, but on build i want to put it in dist/images folder.





updating an existing item in an azure mobile service database


I have a server side script which is trying to update an already existing row with new data that is calculated in the script. When there isn't a row there it adds to the table fine.However when I try to update the row I get an error in my application that tells me that an item with that id already exists. Below is the code I am using for the script. Any tips would be much appreciated.



function insert(item, user, request) {
var table = tables.getTable('Reviews');
table.where({
text: item.id
}).read({
success: upsertItem
});

function upsertItem(existingItems) {
if (existingItems.length == 0) {
item.numReviews = 1;
item.rating = item.reviews;
request.execute();
} else {
item.id = existingItems[0].id;
item.numReviews = existingItems[0].numReviews + 1;
var average = existingItems[0].reviews / item.numReviews;
item.reviews = existingItems[0].reviews + item.reviews;
item.rating = average;
table.update(item, {
success: function(updatedItem) {
request.respond(200, updatedItem)
}
});
}
}


}





Meteor restrict CRUD operations to admins or the document owner


Currently in my meteor app I want to be able to restrict database CRUD operations to only document owners, or for example if they have the admin permission.


I am using the meteor alanning:roles package, could someone please explain how I could best leverage this package for restricting who can create, read or update documents based on their roles, or if they are the owner of the document?