Why is my localized variable keep saying undefined? - wordpress

I am little new to wordpress. I have tried so many different attempts trying to make this work but can seem to resolve the issue.
It keeps saying that ML_MOVIE_LISTING is undefined, when i try to localize it.
Below is my php code and javascript file.
Any help would really appreciated. Thanks!
function admin_scripts (){
wp_enqueue_style("admin-style",plugins_url("style-admin.css",__FILE__));
wp_enqueue_style("jquery-ui","https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/external/jquery/jquery.js");
wp_enqueue_script("main_js",plugins_url("main.js",__FILE__),["jquery","jquery-ui-sortable"]);
wp_localize_script("ml-script","ML_MOVIE_LISTING",[
"token"=>wp_create_nonce("ml-token")
]);
}
add_action("admin_init","admin_scripts");
JS CODE
jQuery(document).ready(function($){
var movie_sort_list = $(".movie-sort-list"),
order_save_msg = $(".order-save-msg"),
order_save_error = $(".order-save-err");
console.log(ML_MOVIE_LISTING);
}

I just found out the reason why it wasn't working was because the name in the first parameter was different then the name of the first paramaeter in
wp_enqueue_script() They both have to be the same as shown below
wp_enqueue_script("main_js",plugins_url("main.js",__FILE__),["jquery","jquery-ui-sortable"]);
wp_localize_script("main_js","ML_MOVIE_LISTING",[
"token"=>wp_create_nonce("ml-token")
]);

Related

Allow int numbers in #textboxfor with no comma or point

So I created a Razor page with a #html.textboxfor that I want to allow only numbers. I changed the type to number, but still, when testing it, it will accept comma and point. I tried some onchange functions to get the value from that textbox, replace all commas or points with nothing, and return to the same textbox. I managed to do that using a second textbox just for testing, but I don't want that behavior. I want the textboxfor to have the same behavior as when I try to type any letters. Which means, do nothing. I just started learning C#/Asp.net, and I couldn't find anything on google. Maybe I'm not searching for the right names, so any tips or directions on what I should research for would be greatly appreciated.
I actually found something that works perfectly. Just in case anyone wants to use it.
First I created the TextboxFor and removed the number type:
#Html.TextBoxFor(i => i.price, new { #id = "amount", #onInput = "javascript: replaceComma('amount'); "})
Then I added this javascript to replace any non-number char with an empty value:
<script type="text/javascript">
function replaceComma(i) {
var val = document.getElementById(i).value;
val = val.replace(/[^0-9]+/g, '')
document.getElementById(i).value = val;
}
</script>
I'm not sure this is the best approach, but it seems to be working now.

Symfony- Calling validation classl using variables

I am trying to do validation in symfony by using variables as shown below
$call= 'Assert\\'.$k2;
//echo $k2.'-'.$item;
echo $call;
//exit;
$arrayConstraint = new $call($item);
//$arrayConstraint = new Assert\NotBlank(null);
$errors = $this->get('validator')->validate(
$arr_item,
$arrayConstraint
);
This code gives error:
Attempted to load class "NotBlank" from namespace "Assert".
Did you forget a "use" statement for "Symfony\Component\Validator\Constraints\NotBlank"?
Whereas I am using the proper namespace(im including the class on top).
Whenevr i uncomment this line
$arrayConstraint = new Assert\NotBlank(null);
and comment this
//$arrayConstraint = new $call($item);
it works perfectly fine.
I guess this has something to do with calling classes using variables. Any ideas for a workaround?
Any help will be deeply appreciated
Finally I did it, by adding the whole path(namespace) to the variable.
$call= "Symfony\\Component\\Validator\\Constraints\\".$k2;
This is working now.

How to stop warnings for functions without DefinitelyTyped?

If some function or library does not have DefinitelyTyped, I know these two ways to stop warnings.
interface Navigator {
getUserMedia: any
}
declare let RTCIceCandidate: any;
But right now, this 3rd-part library Collection2 is used like this:
let ProductSchema = {};
let Products = new Mongo.Collection('products');
Products.attachSchema(ProductSchema);
It give me a warning:
Property 'attachSchema' does not exist on type 'Collection'.
I tried the way below, but it does not work.
interface Collection {
attachSchema: any
}
How can I stop this warning? Thanks
EDIT:
Eric's adding any way solves the problem.
let Products:any = new Mongo.Collection('products');
Products.attachSchema(ProductSchema);
But now a new trouble comes:
let UserSchema = {};
Meteor.users.attachSchema(UserSchema);
Since Meteor.users is built in, so there is no place to add any. How to solve this? Thanks
Thanks for Amid's help. So the way is:
(<any>Meteor.users).attachSchema(UserSchema);

document.querySelectorAll not defined

I am following a calculator tutorial from here: http://thecodeplayer.com/walkthrough/javascript-css3-calculator
However I am using nitrous as the IDE and Meteor. In this part of the code in the js file:
// Get all the keys from document
var keys = **document.querySelectorAll**("#calculator span");
var operators = ['+', '-', 'x', 'รท'];
var decimalAdded = false;
The 'document.querySelector All' part comes up with a not defined error. I have tried replacing it with the more Meteor friendly 'template. find' however then it just says that template is not defined. Any help would be very much appreciated. :)

SDL Tridion GetListKeywords using Anquilla Framework

I'm writing a GUI extension and using the Anquilla framework to get a list of Keywords within a Category. I'm obtaining an XML document for the list of keywords then working with that document within my extension.
My problem is that the returned XML doesn't contain the Keyword's 'Description' value. I have the Title and Key etc.
My original code looks like this:
var category = $models.getItem("CATEGORYTCMID:);
var list = category.getListKeywords();
list.getXml();
A typical node returned is this:
<tcm:Item ID="tcm:4-1749-1024"
Type="1024" Title="rate_one" Lock="0" IsRoot="true"
Modified="2012-12-17T23:01:59" FromPub="010 Schema"
Key="rate_one_value" IsAbstract="false"
CategoryTitle="TagSelector"
CategoryID="tcm:4-469-512" Icon="T1024L0P0"
Allow="268560384" Deny="96" IsNew="false"
Managed="1024"/></tcm:ListKeywords>
So I've tried using a Filter to give me additional column information:
var filter = new Tridion.ContentManager.ListFilter();
filter.columns = Tridion.Constants.ColumnFilter.EXTENDED;
var list = category.getListKeywords(filter);
Unfortunately this only gives the additional XML attributes:
IsShared="true" IsLocalized="false"
I'd really like the description value to be part of this XML without having to create a Keyword object from the XML. Is such a thing possible?
cough any ideas? cough
I'm afraid you'll have to load the Keyword itself to get the Description.
It's not used in any lists, so it's not returned in the XML.
You could always create a List Extender to add this information to the list, but try to be smart about it since this extender will execute everytime a GetList is called.
Won't save you from having to open every keyword in the list, but you'll be doing it server-side (with Core Service/NetTcp for instance) which will probably be easier and faster than opening each keyword with Anguilla.
In this instance I only need the one keyword, so I simply get it from the CMS. Getting an object in Anguilla is a bit weird, here's the code:
In your main code area:
var selectedKy = $models.getItem("TcmUriOfKeywordHere");
if (selectedKy.isLoaded()) {
p.selectedKy = selectedKy;
this.onselectedKyLoaded();
} else {
$evt.addEventHandler(selectedKy, "load", this.onselectedKyLoaded);
selectedKy.load();
}
It's worth noting how I store the keyword in the properties of the item, so I can obtain it in the onselectedKyLoaded function
The function called once the item is loaded
ContentBloom.ExampleGuiExtension.prototype.onselectedKyLoaded = function (event) {
var p = this.properties;
var selectedDescription = p.selectedKy.getDescription();
// do what you need to do with the description :)
};
I resolved this, thanks to the answer here: https://stackoverflow.com/a/12805939/1221032 - Cheers Nuno :)

Resources