I have created a custom trigger with jQuery that accepts two parameters, but the second never arrives.
//firstly
$(Crafty).bind('onTsxLoaded', function (event, tsx, firstSpriteId) {
console.log("tsx", tsx);
console.log("firstspriteid", firstSpriteId);
}
//<snip>
success: function (xml) {
var firstSpriteId = tileset.attr("firstgid");
console.log("done loading ts", firstSpriteId);
$(Crafty).trigger('onTsxLoaded', xml, firstSpriteId);
}
yields:
"done loading ts" 1
"tsx" object
"firstspriteid" undefined
Why is firstspriteid 1 before trigger and undefined right after?
If you want to pass extra parameters to an event using trigger, they need to be in an array.
$(Crafty).trigger('onTsxLoaded', [xml, firstSpriteId]);
Related
I'm trying to do some operations after a successful data binding of the view. The best idea would be in my opinion to do it in the callback of the attachModelContextChange method. After defining which method would be the callback and attaching the event listener to the view, when I try to bind data to my view, I get the error: Cannot read property 'call' of undefined. I'm pasting my controllers code fragments below and the stack trace.
onInit: function() {
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.attachRouteMatched(this.routeMatched, this);
},
routeMatched: function(oEvent) {
var sEntityPath,
oParameters = oEvent.getParameters();
this.routeName = oParameters.name;
this.getView().detachModelContextChange(
this.modelCallback('edit'),
this.getView()
);
if (oParameters.name === "EditItem") {
if (!isNaN(oParameters.arguments.id)) {
this.getView().attachModelContextChange(null,
this.modelCallback('edit'),
this.getView()
);
sEntityPath = "/ItemData(" + oParameters.arguments.id + ")";
this.getView().bindElement(sEntityPath);
} else {
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.navTo("CreateItem", {
from: "EditItem"
}, false);
}
}
},
modelCallback: function(type) {
console.log(type);
}
The console.log from the modelCallback method is launching properly. Removing the line with
this.getView().bindElement(sEntityPath);
prevents the error from triggering (but this way I'm of course not getting any data in my view).
Any ideas what am I missing?
Below I'm pasting the stack trace:
datajs.js:17 Uncaught TypeError: Cannot read property 'call' of undefined
at f.a.fireEvent (sap-ui-core.js:449)
at f.a.fireEvent (sap-ui-core.js:991)
at f.fireModelContextChange (sap-ui-core.js:573)
at f.h.setElementBindingContext (sap-ui-core.js:524)
at constructor.i (sap-ui-core.js:500)
at constructor.a.fireEvent (sap-ui-core.js:449)
at constructor.B._fireChange (sap-ui-core.js:1302)
at eval (library-preload.js:2375)
at u (library-preload.js:2460)
at d.r._processSuccess (library-preload.js:2492)
You have incorrect parameters in attachModelContextChange call. The only required parameter is a callback function, modelCallback in your case. You pass null as oData optional parameter which can be omitted. The correct call should look like this:
this.getView().attachModelContextChange(
this.modelCallback, // the function, not the result of its call!
this // Instance of controller (not View!), which is now *this*
);
Also, the call of detachModelContextChange should be with the same parameters to make it work:
this.getView().detachModelContextChange(
this.modelCallback, // the function, not the result of its call!
this // Instance of controller (not View!), which is now *this*
);
when you place this.modelCallback('edit') at the parameter list then first the function is executed and then its result is passed as a parameter to attachModelContextChange (or detachModelContextChange)
onRowSelect:function(oAnubhav){
//alert("Yes, Row Selection Worked Roger !");
//step1 get the path of the selection item.
debugger;
var sPath= oAnubhav.getParameter("rowContext").getPath();
//step2 transfer the element data to the simpleformcontrol,using bind element.
//2.1 get the object of simpleform-parent of all input.
// var oSimple=this.getView().byId("idSimple");
//2.2the entire simpleform new recives data form table elementselected.
//oSimple.bindElement(sPath);
this.getView().bindElement(sPath);
},
I'm developing a jQuery plugin, and I want to know if there's any possibility to get the second parameter without set the first parameter.
MCVE:
https://jsfiddle.net/2fgb5agL/
//jQuery
$.fn.myPlugin = function(param1,param2) {
return this.each( function() {
if (param2) {
alert("param2 ok");
}
});
};
//Call the plugin
$("#call").click(function() {
$(this).myPlugin("", "xyz");
});
What I mean is: it's possible to call $(element).myPlugin("xyz") and expect the plugin recognize a defined string (ex. "xyz") and then 'do something' (call other function).
So, I don't gonna need to call $(element).myPlugin(undefined,"xyz") for get the second param without setting the first one.
Thanks for reading.
p.s.: What I want to achieve is a cleaner code.
Unless the parameter types are strictly different, you can't even create a distinction inside the function.
Is usual to use a single object parameter as argument like so:
//jQuery
$.fn.myPlugin = function(options) {
return this.each( function() {
if (options.param2) {
alert("param2 ok");
}
});
};
$("#call").click(function() {
$(this).myPlugin({ param1: "", param2: "xyz" });
});
This way you can chose which argument to pass for your plugin. For example: $(this).myPlugin({ param2: "xyz" });
getting error "could not locate bindings file"
trying to search for blog posting, then lookup banner based on blog abbrev, then write out
without banner lookup, everything works fine
think we are screwing up the callback but can't find good example
app.get("/posting/:id", function(req,res) {
var db = app.get('db');
var id = [req.params.id];
console.log(id);
db.run("select * from postings where id=$1",[1], function(err,posting) {
console.log(posting);
var choice = posting[0].choice;
var banner = banner_lookup(choice, function(err, banner) {
console.log(banner);
res.render("posting", {posting:posting[0], banner:banner});
});
});
});
function banner_lookup(abbrev) {
console.log("banner lookup");
for (i=0;i++;i<banners.length) {
if (abbrev == banners[i].abbrev) {
console.log(banners[i]);
return (banners[i]);
}
}
return {"name":"","color":"#000"};
}
var banners = [{"abbrev":"aaa","name":"Newsletter A", "color":"#888888"}];
The problem lies in this part of the program. You are basically passing two arguments to the banner_lookup function - the variable choice and the callback function where you expect the banner to be available after the lookup
var banner = banner_lookup(choice, function(err, banner) {
console.log(banner);
res.render("posting", {posting:posting[0], banner:banner});
});
However the actual function definition of banner_lookup accepts only one named argument - the choice. The banner_lookup function calculates and returns the banner value but it is not being passed to the callback function like you expect it to. You will have to explicitly write the functionality to support callback functionality in banner_lookup
Your code (with callback functionality) would look like this
function banner_lookup(abbrev, callback) {
console.log("banner lookup");
var bannerOutput = {"name":"","color":"#000"};
for (i=0;i++;i<banners.length) {
if (abbrev == banners[i].abbrev) {
console.log(banners[i]);
bannerOutput = banners[i];
}
}
if(typeof callback == "function") {
callback(null, bannerOutput);
} else {
return bannerOutput;
}
}
The above method now accepts a callback parameter, and if it is defined as a function, then passes the output to the function else returns it like earlier.
Tip 1 : You don't really need to implement callback functionality if the banner_lookup function is doing simple tasks in sync. You can simply write
var banner = banner_lookup(choice);
console.log(banner);
This banner will have the expected result.
Tip 2 : And as Amit pointed out in his comment, read about SQL injection attacks. Make sure you sanitize all variables that go into making a DB query (You are directly accepting the id parameter from the route and passing it to the DB query which can be easily misused.
Your function banner_lookup requires only 1 parameter on its decleration, yet you pass it 2 when you call it (choise and the callback function).
Not sure what I am doing wrong here, but let me setup the scenario.
In the beginning, a store(resultsStore) is being populated with data and being displayed via a function call.
Now, an intermediate step of obtaining more data by way of another store is needed and passing both sets to the original function.
I have tried many variations of the below, but the two datasets do not seem to be passed to the updateLeftPanel function, the first one is there fine, the second says "undefined". Anything obvious I am missing?
resultsStore.load({
params: {
'certid' : certId
},
callback: function(record,operation,success){
updateRightPanel(record[0].data); // code not shown,works fine
//updateLeftPanel(record[0].data); // original call
loadCriteriaStore(record[0].data); // new call
}
});
function loadCriteriaStore(data)
{
criteriaStore.load({
params: {
'edition' : data['Edition']
},
callback: function(record,operation,success,data){
updateLeftPanel(data,record[0].data);
// orig data first, new dataset second
}
});
}
function updateLeftPanel(data, dataa){
// code here
dataa object still unpopulated
}
In the callback in the loadCriteriaStore function, you are assigning 4 parameters. I'm assuming that it only passes 3.
So, in that callback, the data that contains your data is being overwritten by a new "local" data, that's undefined.
function loadCriteriaStore(data)
{
criteriaStore.load({
params: {
'edition' : data['Edition']
},
// Get rid of the ",data" here
callback: function(record,operation,success){
// the `data` param will be the value passed to `loadCriteriaStore`
updateLeftPanel(data,record[0].data);
// orig data first, new dataset second
}
});
}
I hope my title isn't too confusing. An example first. I have the following code that configures the read operation for a Kendo UI data source. I am trying to filter all reads based on the selected company id, but my getSelectedCompanyId function is only ever called once, when the page loads. The code below is too long to include all here, so just an excerpt.
$(function () {
function getSelectedCompanyId() {
var id = $("#CompanyId").val();
return id;
}
$("#CompanyId").kendoDropDownList({
change: function () {
grid.dataSource.read();
}
});
var departmentIndexDataSource = new kendo.data.DataSource({
transport: {
read: {
url: '#Url.Action("ListForCompanyIdJson", "Department")' + '?companyId=' + getSelectedCompanyId(),
type: "GET"
},
The ListForCompanyIdJson action is always called with the value selected in the dropdown when $("#CompanyId").kendoDropDownList() is called. I want this function to be called whenever I call grid.dataSource.read().
I realize this might be highly specific to the Kendo stuff, but maybe it's something I can solve with plain JavaScript closures and some help.
You could install a replacement function for grid.dataSource.read() that always calls your function first and then calls grid.dataSource.read().
For example:
grid.dataSource.oldRead = grid.dataSource.read;
grid.dataSource.read = function() {
// call your function here
return grid.datasource.oldRead.apply(this, arguments);
}
Or, if you want to be able to call one sometimes and one other times, you can just make a new method that provides the new behavior and use it when you want to:
grid.dataSource.myread = function() {
// call your function here
return grid.datasource.read.apply(this, arguments);
}