Skip to content
This repository was archived by the owner on Jun 21, 2024. It is now read-only.

Commit b8a0cb4

Browse files
committed
Add .load(), .width() and .height()
1 parent 5029729 commit b8a0cb4

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lib/mquery.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,25 +909,55 @@ export namespace m$ {
909909
* @param index A zero-based integer indicating which element to retrieve.
910910
*/
911911
public get(index?: number): HTMLElement[] | HTMLElement {
912-
if (!isSet(index)) {return makeArray(this); }
913-
if (index < 0) {index = this.length + index; }
914-
return index >= 0 && index < this.length ? this[index] : void 0;
912+
if (!isSet(index)) { return makeArray(this) }
913+
if (index < 0) { index = this.length + index }
914+
return index < this.length ? this[index] : void 0;
915915
}
916916

917+
public width(): number;
917918
public width(value?): mQuery | number {
918919
return size(this, 'Width', value);
919920
}
920921

922+
public height(): number;
921923
public height(value?): mQuery | number {
922924
return size(this, 'Height', value);
923925
}
924926

927+
928+
public load(url: string): this;
929+
public load(url: string, complete: AJAXSuccess): this;
930+
public load(url: string, data: any, complete: AJAXSuccess): this;
931+
public load(url, dataOrComplete?, complete?): this {
932+
// If instance is empty, just return
933+
if (isEmpty(this)) { return this }
934+
935+
// Get parameters
936+
let data = dataOrComplete;
937+
if (!isSet(complete)) {
938+
complete = dataOrComplete;
939+
data = void 0;
940+
}
941+
942+
// Get selector with the url (if exists)
943+
let matches = url.trim().match(/^([^\s]+)\s?(.*)$/),
944+
selector = matches[2];
945+
url = matches[1];
946+
947+
// Request url with data
948+
m$.get(url, data, (data) => {
949+
if (selector) { data = m$(data).filter(selector) }
950+
this.empty().append(data);
951+
}).allways(complete);
952+
return this;
953+
}
954+
925955
/**
926956
* Merge the contents of an object onto the mQuery prototype to provide new mQuery instance methods.
927957
* @param obj An object to merge onto the jQuery prototype.
928958
*/
929959
public extend(obj: Object): void {
930-
each(obj, (key, value) => {fn[key] = value});
960+
each(obj, (key, value) => { fn[key] = value });
931961
}
932962
}
933963

0 commit comments

Comments
 (0)