mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Common UI module implemented & used in cuteness.io
This commit is contained in:
@@ -1,53 +1,49 @@
|
||||
import imageSource = require("image-source");
|
||||
import virtualArray = require("data/virtual-array");
|
||||
import http = require("http");
|
||||
import observable = require("data/observable");
|
||||
import imageCache = require("ui/image-cache");
|
||||
import {ImageSource, fromFile as imageSourceFromFile} from "image-source";
|
||||
import {VirtualArray, ItemsLoading as virtualArrayItemsLoadingData} from "data/virtual-array";
|
||||
import {Observable} from "data/observable";
|
||||
import {Cache as ImageCache} from "ui/image-cache";
|
||||
|
||||
import redditModel = require("./reddit-model");
|
||||
import redditViewModel = require("./reddit-item-view-model");
|
||||
import {Data as RedditData} from "./reddit-model";
|
||||
import {RedditViewModel} from "./reddit-item-view-model";
|
||||
|
||||
var aboutText = "Cuteness is a proof of concept app demonstrating the Telerik's NativeScript for writing native mobile applications using JavaScript.";
|
||||
export var defaultThumbnailImageSource = imageSource.fromFile("~/res/reddit-logo.png");
|
||||
export var defaultNoThumbnailImageSource = imageSource.fromFile("~/res/no-image.png");
|
||||
export var defaultThumbnailImageSource = imageSourceFromFile("~/res/reddit-logo.png");
|
||||
export var defaultNoThumbnailImageSource = imageSourceFromFile("~/res/no-image.png");
|
||||
|
||||
var redditUrl = "http://www.reddit.com/r/aww.json?limit=";
|
||||
var after: string;
|
||||
var ISSCROLLING = "isLoading";
|
||||
|
||||
// initialize the image cache for the main list
|
||||
export var cache = new imageCache.Cache();
|
||||
export var cache = new ImageCache();
|
||||
cache.placeholder = defaultThumbnailImageSource;
|
||||
cache.maxRequests = 5;
|
||||
|
||||
export class AppViewModel extends observable.Observable {
|
||||
export class AppViewModel extends Observable {
|
||||
|
||||
private _redditItems: virtualArray.VirtualArray<redditViewModel.RedditViewModel>;
|
||||
get redditItems(): virtualArray.VirtualArray<redditViewModel.RedditViewModel> {
|
||||
private _redditItems: VirtualArray<RedditViewModel>;
|
||||
get redditItems(): VirtualArray<RedditViewModel> {
|
||||
if (!this._redditItems) {
|
||||
this._redditItems = new virtualArray.VirtualArray<redditViewModel.RedditViewModel>(1000);
|
||||
this._redditItems = new VirtualArray<RedditViewModel>(1000);
|
||||
this._redditItems.loadSize = 50;
|
||||
this._redditItems.on(virtualArray.VirtualArray.itemsLoadingEvent, (args: virtualArray.ItemsLoading) => {
|
||||
this._redditItems.on(VirtualArray.itemsLoadingEvent, (args: virtualArrayItemsLoadingData) => {
|
||||
|
||||
http.getJSON<redditModel.Data>(redditUrl + args.count +
|
||||
(after ? "&after=" + after : "")).then(result => {
|
||||
fetch(redditUrl + args.count + (after ? "&after=" + after : "")).then<RedditData>(response=> response.json()).then(result => {
|
||||
|
||||
var itemsToLoad = result.data.children.map(i=> {
|
||||
return new redditViewModel.RedditViewModel(i.data);
|
||||
});
|
||||
|
||||
this._redditItems.load(args.index, itemsToLoad);
|
||||
|
||||
var lastItem = itemsToLoad[itemsToLoad.length - 1];
|
||||
if (lastItem) {
|
||||
after = itemsToLoad[itemsToLoad.length - 1].source.name;
|
||||
}
|
||||
|
||||
}, (e) => { console.log(e.message) })
|
||||
.catch(function(e) {
|
||||
setTimeout(function() { throw e; });
|
||||
var itemsToLoad = result.data.children.map(i=> {
|
||||
return new RedditViewModel(i.data);
|
||||
});
|
||||
;
|
||||
|
||||
this._redditItems.load(args.index, itemsToLoad);
|
||||
|
||||
var lastItem = itemsToLoad[itemsToLoad.length - 1];
|
||||
if (lastItem) {
|
||||
after = itemsToLoad[itemsToLoad.length - 1].source.name;
|
||||
}
|
||||
|
||||
}).catch(e => {
|
||||
setTimeout(() => { throw e; });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -69,7 +65,7 @@ export class AppViewModel extends observable.Observable {
|
||||
cache.enableDownload();
|
||||
}
|
||||
|
||||
this.notify({ object: this, eventName: observable.Observable.propertyChangeEvent, propertyName: ISSCROLLING, value: value });
|
||||
this.notify({ object: this, eventName: Observable.propertyChangeEvent, propertyName: ISSCROLLING, value: value });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,11 +73,11 @@ export class AppViewModel extends observable.Observable {
|
||||
return aboutText;
|
||||
}
|
||||
|
||||
get defaultThumbnailImageSource(): imageSource.ImageSource {
|
||||
get defaultThumbnailImageSource(): ImageSource {
|
||||
return defaultThumbnailImageSource;
|
||||
}
|
||||
|
||||
get defaultNoThumbnailImageSource(): imageSource.ImageSource {
|
||||
get defaultNoThumbnailImageSource(): ImageSource {
|
||||
return defaultNoThumbnailImageSource;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user