I start with DustJs in KrakenJs environment and i have some troubles with Dust helpers.
In fact, i want to create a helper that can create for me a simple bootstrap button.
Here is my code :
var dust = require('dustjs-linkedin');
if (!dust.helpers)
dust.helpers = {};
dust.helpers.bootstrapButton = function (chunk, context, bodies, params) {
var body = bodies.block || '',
options = params || {},
btnStyle = options.style || 'default',
btnClass = options.class || '',
btnSize = options.size || '';
btnStyle = 'btn btn-' + btnStyle;
if (btnSize)
btnSize = 'btn-' + btnSize;
return chunk.write('<button class="' + btnClass + btnStyle + btnSize + '">' + body + '</button>');
};
And when i call this helper i have the render function for body instead of the final text for body (button content : "function body_3(chk,ctx){ctx=ctx.shiftBlocks(blocks);return chk.write("test");}")
I tried to user chunk.render but i have an error because my final html is not a function like body.
Do you have any idea ?
Regards,
Guillaume
The body is an unevaluated chunk which you need to evaluate before you can concatenate it with your strings.
var curChunk = chunk.data.join(); // Capture anything in chunk prior to this helper
chunk.data = []; // Empty current chunk
var body = bodies.block(chunk).data.join() || '', // Evaluate block and make a string of it
.......
return chunk.write(curChunk + '<button class="' + btnClass + btnStyle + btnSize + '">' + body + '</button>'); // Prefix output with any earlier chunk contents and then build your tag.
Related
I've got the code for Highcharts in combination with Thingspeak from here:
https://forum.arduino.cc/index.php?topic=213058.0
My problem is, I am not able to implement the different suffix into the code :-(. I've tried a lot, but I dont understand the mechanism behind the Java code.
I've tried some things, but result is, I only have one datafield for the first series but not for the other series...
Formatter function is on line 246.
My different yAxies on line 286.
How can formatter decide which yAxies do actual series use?
Maybe somebody have fun to help me?
http://jsfiddle.net/cbmj8rku/
Best regards, David
Each series is assigned to one yAxis. You can detect which series uses which axis for example by axis title:
tooltip: {
formatter: function() {
var points = this.points,
title,
result = '';
Highcharts.each(points, function(p) {
result += p.y;
title = p.series.yAxis.axisTitle.textStr;
if (title === 'yAxis1') {
result += 'suffix1<br>'
} else if (title === 'yAxis1') {
result += 'suffix2<br>'
} else {
result += 'suffix2<br>'
}
});
return result
}
}
Live demo: http://jsfiddle.net/BlackLabel/ncvtxoke/
I changed the code like this:
http://jsfiddle.net/cbmj8rku/20/
formatter: function() {
var d = new Date(this.x + (myOffset*60000));
var _Min = (d.getMinutes()<10) ? '0' + d.getMinutes() : d.getMinutes();
var _Sec = (d.getSeconds()<10) ? '0' + d.getSeconds() : d.getSeconds();
var s = d.getHours() + ':' + _Min + ':' + _Sec + '<br/>';
$.each(this.points, function () {
s += '<br/>' + this.series.name + ' <b>' + this.y + this.series.yAxis.userOptions.labels.suffix + '</b>';this.series.tooltipOptions.valueSuffix[this.point.index];
});
return s;
}
I have below procedure in Document DB. It executes fine from DocumentDb script explorer but the result it returns is partial. I have more than 250 documents satisfying its given where clause which I checked in query explorer. But when I run procedure from script explorer count(defined in procedure) is always 100.
Below is my procedure -
function getInvoice(pageNo, numberOfRecords, member, searchText, customerGroupId,ResellerId) {
var collectionReseller = getContext().getCollection();
var filterquery ;
var count=0, invoiceAmountTotal=0, referalCommissionTotal=0, developerCommissionTotal=0;
var customerIdString='';
var InvoiceList = [];
var whereClause;
if (customerGroupId != "") {
filterquery = 'SELECT c.id from c where c.Type="Customer" and c.CustomerGroupID="' + customerGroupId + '"';
var isAccepted = collectionReseller.queryDocuments(
collectionReseller.getSelfLink(), filterquery,
function (err, documents, responseOptions) {
var docCount = documents.length;
documents.forEach(function (doc) {
docCount--;
if (docCount > 0)
customerIdString = customerIdString + '"' + doc.id + '", '
else
customerIdString = customerIdString + '"' + doc.id + '" '
})
whereClause = 'where r.Type="Invoice" and r.CustomerID IN (' + customerIdString + ')'
var filterquery1 = 'SELECT * FROM root r ';
if (member.length > 0) {
member.forEach(function (val, i) {
whereClause = whereClause + ' and contains(r.' + member[i] + ',"' + searchText[i] + '")';
});
}
isAccepted = collectionReseller.queryDocuments(
collectionReseller.getSelfLink(), filterquery1 + whereClause,
function (err, invoiceDoc) {
var qr = filterquery1 + whereClause;
count = invoiceDoc.length;
invoiceDoc.forEach(function (doc) {
invoiceAmountTotal = parseFloat(invoiceAmountTotal) + parseFloat(doc.InvoiceAmount);
referalCommissionTotal = parseFloat(referalCommissionTotal) + parseFloat(doc.ReferralCommission);
developerCommissionTotal= parseFloat(developerCommissionTotal) + parseFloat(doc.DeveloperCommission);
InvoiceList.push(doc);
});
InvoiceList.sort(SortByID);
InvoiceList = InvoiceList.slice(pageNo * numberOfRecords, pageNo * numberOfRecords + numberOfRecords);
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
getContext().getResponse().setBody(JSON.stringify({ InvoiceList, count, invoiceAmountTotal, referalCommissionTotal, developerCommissionTotal }));
});
});
}
else
{
whereClause = ' where r.Type = "Invoice" and r.ResellerID = "'+ ResellerId + '"';
filterquery = 'SELECT * FROM root r ';
if(member.length > 0) {
member.forEach(function (val, i) {
whereClause = whereClause + ' and contains(r.' + member[i] + ',"' + searchText[i] + '")';
});
}
filterquery = filterquery + whereClause;
var isAccepted = collectionReseller.queryDocuments(
collectionReseller.getSelfLink(), filterquery,
function (err, documents, responseOptions) {
if (err) throw err;
invoiceDoc = documents;
count =invoiceDoc.length;
invoiceDoc.forEach(function (doc) {
InvoiceList.push(doc);
invoiceAmountTotal = parseFloat(invoiceAmountTotal) + parseFloat(doc.InvoiceAmount);
referalCommissionTotal = parseFloat(referalCommissionTotal) + parseFloat(doc.ReferralCommission);
developerCommissionTotal= parseFloat(developerCommissionTotal) + parseFloat(doc.DeveloperCommission);
});
InvoiceList.sort(SortByID);
InvoiceList = InvoiceList.slice(pageNo * numberOfRecords, pageNo * numberOfRecords + numberOfRecords);
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
getContext().getResponse().setBody(JSON.stringify({ InvoiceList, count, invoiceAmountTotal, referalCommissionTotal, developerCommissionTotal }));
});
}
function SortByID(a, b) {
var aName = a.UpdatedOn.toLowerCase();
var bName = b.UpdatedOn.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
Any help will be highly appreciated..
If you want to get all 250 back in one shot, you need to populate the options parameter for queryDocuments() with a pageSize field. It's an optional third parameter for that function call. Without it, this server-side API will default to 100.
You can also set pageSize to -1 to get you all documents. However, for server-side stored procedures, I recommend against this. Rather, you need to handle paging using the continuation token. If you want it to be really robust you also need to deal with premature shutdown of the stored procedure.
I am automating an application where I need to verify the cover Image of a book.
I encoutered a situation where the cover image changed and my script was not able to report this, since the the image source remained same.
You could check that the hash of the targeted image doesn't change. Here is an example to compute the hash of an image with Selenium / Python:
from selenium import webdriver
JS_GET_IMAGE_HASH = """
var hash = 0, ele = arguments[0], xhr = new XMLHttpRequest();
var src = ele.src || window.getComputedStyle(ele).backgroundImage;
xhr.open('GET', src.match(/https?:[^\"')]+/)[0], false);
xhr.send();
for (var i = 0, buffer = xhr.response; i < buffer.length; i++)
hash = (((hash << 5) - hash) + buffer.charCodeAt(i)) | 0;
return hash.toString(16).toUpperCase();
"""
driver = webdriver.Firefox()
driver.get("https://www.google.co.uk/")
# get the logo
ele_image = driver.find_element_by_id("hplogo")
# compute the hash of the logo
image_hash = driver.execute_script(JS_GET_IMAGE_HASH, ele_image)
# print the hash code
print image_hash
Or with Selenium / Java:
final String JS_GET_IMAGE_HASH =
"var hash = 0, ele = arguments[0], xhr = new XMLHttpRequest(); " +
"var src = ele.src || window.getComputedStyle(ele).backgroundImage; " +
"xhr.open('GET', src.match(/https?:[^\"')]+/)[0], false); " +
"xhr.send(); " +
"for (var i = 0, buffer = xhr.response; i < buffer.length; i++) " +
" hash = (((hash << 5) - hash) + buffer.charCodeAt(i)) | 0; " +
"return hash.toString(16).toUpperCase(); ";
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://www.google.co.uk/");
// get the logo
WebElement ele_image = driver.findElement(By.id("hplogo"));
// compute the hash of the logo
String image_hash = (String)js.executeScript(JS_GET_IMAGE_HASH, ele_image);
// print the hash code
System.out.println(image_hash);
You could solve this without any image processing by calculating file checksums.
Is there a way to get the cookie,set in JavaScript code, inside a controller method in Ruby on Rails v 4.0 ?
In a controller, you can set a cookie with:
cookies['foo'] = 'bar'
To set a cookie in Javascript, I wrote a short function in Coffeescript:
set_cookie: ( name, value, expiredays = 0 ) ->
expiredate = new Date()
expiredate.setDate expiredate.getDate() + expiredays
expire = '; expires=' + expiredate.toUTCString()
value = escape( value ) + expire
document.cookie = name + '=' + value + '; path=/'
I want to make the following :
1. I have the following formatter function:
function ActionDescriptionFormatter(cellval, opts, rwdat, _act) {
var str = "<a border='0' style='text-decoration: none;' href='/Admin/IdeaDescription?id=" + cellval + "' title='Description'><img src='/images/aico_descr.png' alt='Description' border='0' /></a>";
return str;
}
I want to add also current pagenumber to url.
I want to set page of grid if It's passed via url
How to do it?